mysql string group_concat giving me an error - mysql

i'm getting an error here when i try this sql ...
SELECT customers.customers_first_name GROUP_CONCAT(customers_groups.customers_hash SEPARATOR '')
FROM customers INNER JOIN customers_groups ON (customers.hash = customers_groups.customers_hash)
GROUP BY customers.customers_entry_date
CUSTOMERS DATABASE
`customers_id`,
`customers_first_name`,
`customers_surname`,
`customers_telephone`,
`customers_email`,
`customers_telephone_active`,
`customers_email_active`,
`client_type`,
`customers_hash`,
`customers_entry_date`
CUSTOMERS_GROUPS
`groups_hash`
`customers_hash

Ideally, you should post the error message with the question but check below solution.
SELECT customers.customers_first_name, GROUP_CONCAT(customers_groups.customers_hash SEPARATOR '')
FROM customers INNER JOIN customers_groups ON (customers.hash = customers_groups.customers_hash)
GROUP BY customers.customers_entry_date
SELECT customers.customers_first_name ,
"," was missing in the select statement before GROUP_CONCAT

Related

what is wrong with this sql query that used order by and left join together? Is this the right way to do it?

SELECT ts.contact_id as contact_id, ts.contact_name as name , ts.is_contact_active as is_active, ts.created_at,ts.updated_at
FROM tb_summary ts
ORDER by created_at
LEFT JOIN (tcs.contact_address as address FROM tb_contacts tcs) ON tcs.contact_id = ts.contact_id
WHERE ts.business_owner_id = ? AND ts.is_contact_active = ?
This query throws
ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN tcs.Contact_address as address, tcs.Contact_id FROM tb_Contacts tcs ' at line 2
Your syntax is way off. A query takes only one from clause, that may contain multiple joins. And the order by clause goes at the end.
So:
SELECT
ts.contact_id as contact_id,
ts.contact_name as name,
ts.is_contact_active as is_active,
ts.created_at,ts.updated_at,
tc.contact_address as address
FROM tb_summary ts
LEFT JOIN tb_contacts tc ON tc.contact_id = ts.contact_id
WHERE ts.business_owner_id = ? AND ts.is_contact_active = ?
ORDER by ts.created_at
A SELECT statement consists of a sequence of clauses. Your query has four clauses:
SELECT
FROM
WHERE
ORDER BY
These operators must be in this order.
JOIN is an operator in the FROM clause. So, it should be structure more like this:
SELECT ts.contact_id as contact_id, ts.contact_name as name, ts.is_contact_active as is_active, ts.created_at, ts.updated_at
FROM tb_summary ts LEFT JOIN
tcs.contact_address address tca
ON tca.contact_id = ts.contact_id
WHERE ts.business_owner_id = ? AND ts.is_contact_active = ?
ORDER by created_at
From what I can tell, the query is still non-sensical. The contact_address table is not being used.

Querying data with list of ids from different table

I'm trying to do something like thisin MySQL. Query on one table with a list of ids from another table. The Group_Concat return a list of comma separated ids that i want to use in my first select. My try results in an error.
(Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IN ( SELECT GROUP_CONCAT(t.id SEPARATOR ',') FROM m.my_aspnet_detai' at line 2)
How does one write this with correct syntax?
SELECT * FROM m.my_aspnet_membership m
where m.userId = IN
(
SELECT GROUP_CONCAT(t.id SEPARATOR ',')
FROM m.my_aspnet_details t
WHERE t.customerid = '2'
);
No need to GROUP_CONCAT
SELECT * FROM m.my_aspnet_membership m
where m.userId = IN
(
SELECT t.id
FROM m.my_aspnet_details t
WHERE t.customerid = '2'
);
Or even better to use JOIN
SELECT *
FROM m.my_aspnet_membership
INNER JOIN m.my_aspnet_details t ON t.id=m.userId
WHERE t.customerid = '2'

duplicate result in group_concat

I have this data in my database(3,15,6,4,15), and I tried to show it on my table using group concat, the problem is i got duplicate results. (3,15,6,4,15,3,15,6,4,15,3,15,6,4,15).
I tried to use distinct but it eliminate also the other "15".
What is the best solution for that?
thanks!
this is my query
SELECT users.*, GROUP_CONCAT(written.score separator ' - ') as Wscore, student_subject.*,SUM(written.score) as total, SUM(written.item) as item FROM users JOIN written ON users.idnumber=written.idnumber JOIN student_subject ON users.idnumber=student_subject.idnumber WHERE student_subject.teacher='$login_session' AND written.section='$section' AND written.level='$level' AND written.year='$year' AND written.subject='$subject' AND users.gender='male' AND written.period='first' GROUP BY users.idnumber order by users.lname
You probably just want distinct in the group_concat():
SELECT u.*, GROUP_CONCAT(distinct w.score separator ' - ') as Wscore,
ss.*, SUM(w.score) as total, SUM(w.item) as item
FROM users u JOIN
written w
ON u.idnumber = w.idnumber JOIN
student_subject ss
ON u.idnumber = ss.idnumber
WHERE ss.teacher = '$login_session' AND w.section='$section' AND
w.level = '$level' AND w.year = '$year' AND w.subject = '$subject' AND
u.gender = 'male' AND w.period = 'first'
GROUP BY u.idnumber
order by u.lname;
Notice how table aliases make the query easier to write and to read.

MySQL - Group BY GROUP_CONCAT

I have the following query:
SELECT
issue.`sequence` AS issue_sequence,
issue.`description` AS issue_description,
GROUP_CONCAT(DISTINCT(issue_category.`name`) SEPARATOR ', ') AS issue_category_name,
GROUP_CONCAT(DISTINCT(approach.`name`) SEPARATOR ', ') AS approach_name,
issue_approach.`issue_id` AS issue_approach_issue_id,
issue_approach.`approach_id` AS issue_approach_approach_id
FROM
`approach` approach
INNER JOIN `issue_approach` issue_approach ON approach.`id` = issue_approach.`approach_id`
INNER JOIN `issue` issue ON issue_approach.`issue_id` = issue.`id`
INNER JOIN `project` project ON issue.`project` = project.`id`
INNER JOIN `tenant` tenant ON project.`tenant_id` = tenant.`id`
INNER JOIN `issue_category` issue_category ON project.`id` = issue_category.`project`
INNER JOIN `user` user ON tenant.`id` = user.`tenant_id`
WHERE user.id = 1 AND project.id = 1
GROUP BY issue_category_name
ORDER BY issue_category.`name`, issue.`sequence`
I am having a problem with this line:
GROUP BY issue_category_name
Apparently, MySQL can't seem to Group by the alias for by GROUP_CONCAT result.
I am not really an expert with SQL, but is there a way I can group using the result of GROUP_CONCAT?
Sample data;
Categories: Network, Servers
Issue Id: 1
Description: Some description
Approaches: Some approaches to resolve the issue.
Basically, an issue can belong to one or many categories. I am concatenating categories for each issue. What i want to do is group the results by the result of concatenation of categories. So for example group issues whose categories are Network,Servers.
Thanks.
Im not a MySQL user, but change your group by to
Group By GROUP_CONCAT(DISTINCT(issue_category.`name`) SEPARATOR ', ')
With reference to SQL EXECUTION ORDER, the reason why this will not work is because the select statement is the last statement to be executed so that sql engine will not be knowing your column alias while grouping the records as GROUP BY occurs before SELECT. So that as Charles Bretana's answer suggests, put
Group By GROUP_CONCAT(DISTINCT(issue_category.`name`) SEPARATOR ', ')
in your group by clause. It will work fine then.
Hope this helps you.

SQL: Display multiple record values in one field from a subquery

I have an SQL LIKE:
SELECT S.*,
(SELECT I.NAME FROM institution I, inst_map IM
WHERE IM.STUDENT = S.ID AND IM.INSTITUTION = I.ID) as INSTITUTIONS
FROM student S
In this case it is possible for my subquery to return multiple records (I will get an error: Subquery returns more than 1 row).
How to show those multiple values from my subquery in one field (in my case INSTITUTIONS ) separated by commas?
All ideas are welcome.
Try this query -
SELECT s.*, GROUP_CONCAT(t.NAME) INSTITUTIONS FROM student s
LEFT JOIN (SELECT * FROM institution i
JOIN inst_map im
ON im.INSTITUTION = i.ID
) t
ON s.ID = t.STUDENT
GROUP BY s.ID
The GROUP_CONCAT function will help you to get values separated by commas.
DECLARE #List VARCHAR(5000)
SELECT #List = COALESCE(#List + ', ' + Display, Display)
FROM TestTable
Order By Display
query is taken from following link, and the article explain the query perfectly, I hope it works
http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/289/creating-comma-separated-list-in-sql.aspx
P.S Its for SQL Server, i guess