GROUP_CONCAT DISTINCT , DISTINCT not work - mysql

SELECT
GROUP_CONCAT( DISTINCT unit.`name` ORDER BY unit.`order_index` ASC SEPARATOR '/' ) AS nameGroup
FROM
pms_spec_unit unit
JOIN pms_spec spec ON spec.id = unit.spec_id
WHERE
spec.`status` = 1
GROUP BY
unit.spec_id
HAVING
nameGroup LIKE '%年%'
duplicate result
The results I got
mysql version 8.0.25

I solved this issue thanks to Akina's comment. Actually unit.spec_id is different. Use SELECT DISTINCT GROUP_CONCAT(....

Related

Mysql: Using where comparison operator in outer query against select subquery result [duplicate]

This question already has answers here:
Unknown Column In Where Clause
(16 answers)
Closed 3 years ago.
I am am trying to use a WHERE clause based on a scalar subquery result. The query executes correctly without the WHERE clause. With the WHERE clause I get Error Code: 1054. Unknown column 'available_services' in 'where clause'.
How can I achieve filtering based on the result of the subquery?
Also since subqueries can be pretty inefficient any suggestions of how to improve the query would be useful.
SELECT DISTINCT
`suppliers`.`id` AS `supplier_id`,
`suppliers`.`name`,
`suppliers`.`code`,
`suppliers`.`notes`,
(
SELECT GROUP_CONCAT(
`services`.`name`
ORDER BY `services`.`order`
SEPARATOR ', '
)
FROM `supplier_services`
LEFT JOIN `services`
ON `supplier_services`.`service_id` = `services`.`id`
WHERE
`supplier_services`.`service_id` = `services`.`id`
AND `supplier_services`.`supplier_id` = `suppliers`.`id`
GROUP BY `supplier_services`.`supplier_id`
) AS `available_services`
FROM `suppliers`
WHERE `available_services` like '%pet%'
GROUP BY `suppliers`.`id`
Hi you should as variable in query if would you like to reach subquerys result
SELECT DISTINCT
`suppliers`.`id` AS `supplier_id`,
`suppliers`.`name`,
`suppliers`.`code`,
`suppliers`.`notes`,
#available_services := (
SELECT GROUP_CONCAT(
`services`.`name`
ORDER BY `services`.`order`
SEPARATOR ', '
)
FROM `supplier_services`
LEFT JOIN `services`
ON `supplier_services`.`service_id` = `services`.`id`
WHERE
`supplier_services`.`service_id` = `services`.`id`
AND `supplier_services`.`supplier_id` = `suppliers`.`id`
GROUP BY `supplier_services`.`supplier_id`
) AS `available_services_as_column_view`
FROM `suppliers`
WHERE #available_services like '%pet%'
GROUP BY `suppliers`.`id`
In case anyone comes across this and has a similar problem the reason (as pointed out by Nico in the comments) is that while you can use table aliases in where clauses you can not use field aliases. You can however have field aliases in HAVING clauses.
The solution is to use
GROUP BY `suppliers`.`id`
HAVING `available_services` like '%pet%'
instead of
WHERE `available_services` like '%pet%'
GROUP BY `suppliers`.`id`
Alternatively the alias in the where clause could be replaced with the subquery again but that may be inefficient or it may be cached by mysql and not be an issue, you would need to check that carefully if you use that solution.

SQLITE request syntax error

I have an issue with a request, it works fine with pgsql and mysql, but with sqlite, it seems like I am missing something
here is the request :
select * from mesure_insitu
where (id_formulaire, gid) IN (
select distinct id_formulaire, max(gid) as gid
from mesure_insitu
where id_dispo_comp_ouvr = 1
GROUP BY id_formulaire
ORDER BY id_formulaire ASC
)
This subquery below works fine:
select distinct id_formulaire, max(gid) as gid
from mesure_insitu
where id_dispo_comp_ouvr = 1
GROUP BY id_formulaire
ORDER BY id_formulaire ASC)
I think the problem is with the condition with two values (id_formulaire, gid), like sqlite cannot use a condition with two values.
I will appreciate any type of help.
To get this query to work, update your SQLite to a newer version.
If you cannot do this, you have to use a join instead:
SELECT *
FROM mesure_insitu
JOIN (SELECT id_formulaire, max(gid) AS gid
FROM mesure_insitu
WHERE id_dispo_comp_ouvr = 1
GROUP BY id_formulaire)
USING (id_formulaire, gid);
(The DISTINCT and ORDER BY are superfluous in this subquery.)

Can I Select Distinct inside a where and get all selects in mysql?

I have this:
SELECT FirstName
FROM MAIN_TABLE_LEVEL100
WHERE FirstName IN
(
SELECT DISTINCT FirstName
FROM MAIN_TABLE_LEVEL100
)
ORDER BY `level` DESC, `time` DESC LIMIT 0,10
Im trying to get top 10 diferents names order by level and time, but it doesn't work, if someone can help me please, thanks.
Your where clause is doing nothing, you just repeated the same select that you had above. As long as level and time are in 'MAIN_TABLE_LEVEL100' you don't even need a where clause. This should work:
SELECT DISTINCT FirstName, level, time
FROM MAIN_TABLE_LEVEL100
ORDER BY level DESC, time DESC LIMIT 10
Why don't use GROUP BY .DISTINCT work only with indexed field in some situation.
You can have multiple lines because of time. Try rolling the data together first.
I'm not familiar with mySql, but using generic SQL syntax it would be something like this:
SELECT b.FirstName
FROM (SELECT a.FirstName, a.Level, MAX(a.Time) AS tm FROM MAIN_TABLE_LEVEL100 as a GROUP BY a.FirstName, a.Level) AS b
ORDER By b.Level, b.tm
Thanks everyone, I finally found the right answer!:
SELECT o.FirstName, o.Level, o.Time, o.City
FROM `MAIN_TABLE_LEVEL100` o # 'o' from 'bigger points'
LEFT JOIN `MAIN_TABLE_LEVEL100` b # 'b' from 'bigger after o'
ON o.FirstName = b.FirstName AND o.Points < b.Points
WHERE b.Points is NULL
ORDER BY `o`.`Points` DESC
LIMIT 0,10
thanks everyone, i hope someone need it ;)

mysql DISTINCT() does not work as expected

I want to get list of posts sorted by number of comments, I've successfully ran following query but it gives repetitive values i.e posts repeat, I want unique of them sorted by number of comments, when I put DISTINCT() around my whole query, an error appears:
#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 'UNIQUE(post.pname, post.pid FROM post, COMMENT WHERE post.pid = comment.pid ORD' at line 1
Query without DISTINCT() (Works but of course doesn't give unique values)
SELECT post.pname, post.pid
FROM post,
COMMENT WHERE post.pid = comment.pid
ORDER BY (
SELECT COUNT( * )
FROM COMMENT WHERE comment.pid = post.pid
GROUP BY post.pname
)
Query with DISTINCT() (doesn't work)
SELECT DISTINCT(post.pname, post.pid
FROM post,
COMMENT WHERE post.pid = comment.pid
ORDER BY (
SELECT COUNT( * )
FROM COMMENT WHERE comment.pid = post.pid
GROUP BY post.pname
))
DISTINCT should be used thus:
SELECT DISTINCT a,b,c FROM t;
without a GROUP BY. It will find all the (a,b,c) in the table, then de-dup them.
This is broken:
SELECT id, a, b FROM t GROUP BY id;
That is because it will find all the distinct values of id, but supply random values of a and b to go with each.
To find out how many of each foo there are, this pattern works nicely:
SELECT foo, COUNT(*) FROM t GROUP BY foo;
Don't use () after DISTINCT.
Since I don't understand what you are looking for, I may or may not have provided you enough info to fix your query. If I have failed, please provide some sample data and the desired output; sometimes reverse engineering is the easiest way to figure it out.
SELECT distinct(post.pname) FROM post,COMMENT WHERE post.pid = comment.pid ORDER BY (SELECT COUNT( * ) FROM COMMENT WHERE comment.pid = post.pid GROUP BY post.pname) DESC
SELECT post.pname, post.pid
FROM post
Inner Join ( Select
comment.pid
, COUNT(*) As Cant
From COMMENT
Group By
comment.pid
) As x
On post.pid = x.pid
ORDER BY x.Cant
SELECT p.*
FROM post p JOIN comment c ON c.postId = p.id
GROUP BY p.id
ORDER BY COUNT(*)

group concat from subquery very slow

Coming from here sql group_concat and subquery
I managed to "solve" de problem by doing this a "subquery":
SELECT
GROUP_CONCAT(name,',',results separator '#')
as finalresult
FROM
(
select t.name as name, group_concat(distinct r.idResult separator '-') as results
from threshold t
left join threshold_results r on r.idThreshold = t.idThreshold
group by t.idThreshold, t.name
) final
But it is too slow when there are many records, withouth the subquery the initial solution performs very well. Any ideas?
Thank you!
add index on r.idThreshold, t.name