mysql group_concat function using sub-query in condition - mysql

mysql group_concat function using sub-query in condition I want to try as below :
SELECT * FROM prologic WHERE ID IN (
SELECT GROUP_CONCAT( DISTINCT ID ) from (
SELECT GROUP_CONCAT( DISTINCT ID )ID
FROM `prologic`
GROUP BY Vendor_Code
HAVING COUNT( * ) >1
)c
)
Please guide me to solve it.

SELECT * FROM prologic WHERE ID IN (
SELECT DISTINCT ID FROM prologic
GROUP BY Vendor_Code
HAVING COUNT( * ) >1
)

Related

Solution to MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

For some reason MYSQL doesn't support LIMIT inside a subquery:
SELECT m.*
FROM `my_table` m
WHERE m.`id` IN (
SELECT o.`id`
FROM (SELECT DISTINCT i.`id`, i.`label`, i.`client`, i.`place`
FROM `my_table` i
ORDER BY i.`label`, -i.`client` DESC, -i.`place` DESC) o
WHERE m.`label` = o.`label` LIMIT 1
);
I've tried using join from this link: INNER JOIN INSTEAD OF IN(LIMIT error) but not succeeded. Has anyone any clues for it? Thanks.
Since the subquery returns only 1 row with 1 column there is no need for IN.
You can use =:
SELECT m.*
FROM `my_table` m
WHERE m.`id` = (
SELECT o.`id`
FROM (
SELECT DISTINCT i.`id`, i.`label`, i.`client`, i.`place`
FROM `my_table` i
ORDER BY i.`label`, -i.`client` DESC, -i.`place` DESC) o
WHERE m.`label` = o.`label` LIMIT 1
);
But as it is written, your query uses LIMIT without ORDER BY (you do use ORDER BY in the inner subquery where it is useless).
Do you mean to do something like this:
SELECT m.*
FROM `my_table` m
WHERE m.`id` = (
SELECT o.`id`
FROM (
SELECT DISTINCT i.`id`, i.`label`, i.`client`, i.`place`
FROM `my_table` i
) o
WHERE m.`label` = o.`label`
ORDER BY o.`label`, -o.`client` DESC, -o.`place` DESC
LIMIT 1
);
Also ordering by the negative value of a column descending is equivalent to ordering just ascending, so the ORDER BY clause can be simplified to:
ORDER BY o.`label`, o.`client`, o.`place`

MySQL: sum values from subqueries

Is it possible to sum two values from subqueries?
I need select three values: total_view, total_comments and rating.
Both subqueries is very complicated, so i don't wish duplicate it.
My query example:
SELECT p.id,
(
FIRST subquery
) AS total_view,
(
SECOND subquery
) AS total_comments,
(
total_view * total_comments
) AS rating
FROM products p
WHERE p.status = "1"
ORDER BY rating DESC
I would suggest using a subquery:
SELECT p.*, (total_view * total_comments) as rating
FROM (SELECT p.id,
(FIRST subquery) AS total_view,
(SECOND subquery) AS total_comments,
FROM products p
WHERE p.status = '1' -- if status is a number, then remove quotes
) p
ORDER BY rating DESC;
MySQL materializes the subquery. But because the ORDER BY is on a computed column, it needs to sort the data anyway, so the materialization is not extra overhead.
You can't use alias but you can use the same code eg:
SELECT p.id,
(
FIRST subquery
) AS total_view,
(
SECOND subquery
) AS total_comments,
(
(
FIRST subquery
) * (
SECOND subquery
)
) AS rating
FROM products p
WHERE p.status = "1"
ORDER BY rating DESC
Simply use a Derived Table to be able to reuse the aliases:
SELECT p.id,
total_view,
total_comments,
total_view * total_comments AS rating
FROM
(
SELECT p.id,
(
FIRST subquery
) AS total_view,
(
SECOND subquery
) AS total_comments
FROM products p
WHERE p.status = "1"
) as dt
ORDER BY rating DESC

MySQL - Ordering by a factor obtained by moltiplying a related column by a hABTM

I have 4 tables, photos, tags, photos_tags and visits.
I have to select the first 30 photos ordered by a factor that should be obtained by the sum of the coefficent in the visits table.
I'm trying this query, but it fails saying "every derivated table must have an alias"
SELECT * FROM (
SELECT *, (SELECT SUM(count) FROM (SELECT * FROM visits AS Visits WHERE Visits.tag_id IN (
SELECT tag_id FROM photos_tags AS PhotosTags WHERE PhotosTags.photo_id = Photo.id
)))
) AS computed FROM photos AS Photo WHERE Photo.id NOT IN (
) ORDER BY computed LIMIT 30
) ORDER BY RAND()
EDIT: Query I am trying that throws #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 ') AS Table2 ORDER BY computed LIMIT 30
) AS Table3 ORDER BY RAND()' at line 10
SELECT * FROM (
SELECT *, (
SELECT SUM(Table1.count) FROM (
SELECT * FROM visits AS Visits WHERE Visits.tag_id IN (
SELECT tag_id FROM photos_tags AS PhotosTags WHERE PhotosTags.photo_id = Photo.id
)
) AS Table1
) AS computed FROM photos AS Photo WHERE Photo.id NOT IN (
) AS Table2 ORDER BY computed LIMIT 30
) AS Table3 ORDER BY RAND()
EDIT2 Query that throws #1054 - Unknown column 'Photo.id' in 'where clause'
SELECT * FROM (
SELECT *, (
SELECT SUM(Table1.count) FROM (
SELECT * FROM visits AS Visits WHERE Visits.tag_id IN (
SELECT tag_id FROM photos_tags AS PhotosTags WHERE PhotosTags.photo_id = Photo.id
)
) AS Table1
) AS computed FROM photos AS Photo WHERE photos.id NOT IN (
1,2
) ORDER BY computed LIMIT 30
) AS Table3 ORDER BY RAND()
I think this is the query you want:
SELECT *
FROM (SELECT p.*, computed
FROM Photos AS p
LEFT JOIN (SELECT pt.photo_id, SUM(count) total
FROM photos_tags pt
JOIN visits v
ON v.tag_id = pt.tag_id
GROUP BY photo_id) AS pt
ON p.id = pt.photo_id
where p.id NOT IN ( )
ORDER BY computed
LIMIT 30) AS t1
ORDER BY RAND()

MAX aggregate function in sql server

I wrote the following query to return the the records with the latest date.
select fs.company_id, max(fs.create_dt) as latestcreatedate
from field_sale fs
group by fs.company_id
order by fs.company_id
The query all works fine. But I need to retrieve the record with all related columns attached to it. Such as, id, title, desc and etc.
How can I retrieve the records with its corresponding columns?
Couple ways of doing so :
-- 1.
SELECT a.*
FROM field_sale a
INNER JOIN
(
select fs.company_id, max(fs.create_dt) as latestcreatedate
from field_sale fs
group by fs.company_id
)b
ON b.company_id = a.company_id AND b.latestcreatedate = a.create_dt
order by a.company_id;
-- 2.
SELECT b.* FROM
(
SELECT a.* , ROW_NUMBER()
OVER (PARTITION BY a.company_id ORDER BY a.create_dt DESC)
AS rn
FROM field_sale a
)b WHERE b.rn = 1
ORDER BY company_id
WITH t AS (
SELECT fs.company_id,
fs.create_dt AS latestcreatedate,
id,
title,
etc,
ROW_NUMBER() OVER ( PARTITION BY fs.company_id ORDER BY fs.create_dt DESC ) AS rowNum
FROM field_sale fs
)
SELECT t.company_id,
t.latestcreatedate,
t.id,
t.title,
t.etc
FROM t
WHERE t.rowNum = 1
ORDER BY t.company_id

SQL order by variable in inner statement

I have a MySQL query like this:
SELECT *
FROM backstage
WHERE backstage_id IN (
SELECT backstage_id
FROM visitor_counter
WHERE backstage_id !=0
GROUP BY backstage_id
ORDER BY COUNT( DISTINCT ( ip_address ) ) DESC
)
LIMIT 0 , 100
I get the results I want, but I would like to order it by COUNT( DISTINCT ( ip_address ) ) DESC as the inner question does.
Any tips on how to do this?
Give this a go and see if it gives you what you're after:
select bs.*
from backstage bs
inner join
(
select backstage_id,count(distinct ip_address) as distIpCount
from visitor_counter
where backstage_id !=0
group by backstage_id
) vc on vc.backstage_id = bs.backstage_id
order by vc.distIpCount desc
limit 0,100;