Here is my MySQL database:
And my query to get all subcategories:
SELECT `a`.`id`, `a`.`name`, `a`.`url_segment`, `a`.`categories_id`, `b`.`image` AS supplements_image
FROM (`subcategories` AS a)
LEFT JOIN `supplements` AS b ON `b`.`subcategories_id` = `a`.`id`
LEFT JOIN `reviews` AS c ON `c`.`supplements_id` = `b`.`id`
GROUP BY `a`.`id`
ORDER BY `a`.`categories_id` ASC, COUNT(c.id) DESC, `b`.`image` ASC
The problem is with the subcategory image, that should be the image of a random product inside that subcategory, but I'm always getting the image of the first product. Any idea how I can do this?
SELECT `a`.`id`, `a`.`name`, `a`.`url_segment`, `a`.`categories_id`,
(SELECT `image`
FROM `supplements`
WHERE `subcategories_id` = `a`.`id`
ORDER BY RAND() LIMIT 1
) AS supplements_image
FROM (`subcategories` AS a)
LEFT JOIN `reviews` AS c ON `c`.`supplements_id` = `a`.`id`
GROUP BY `a`.`id`
ORDER BY `a`.`categories_id` ASC, COUNT(c.id) DESC
Edited unspecified alias b and now works great: See fiddle!
Related
Is it possible to convert this subquery to join?
SELECT `news`.`newsId`,
(SELECT `comments`.`text`
FROM `comments`
WHERE `comments`.`newsId` = `news`.`newsId`
order by `comments`.`date` desc
limit 1)
FROM `news` , `comments`
where `news`.`newsId` = `comments`.`newsId`
GROUP BY `news`.`newsId`
order by news.date desc;
I assume newsId is unique.
SELECT `news`.`newsId`,
`comments`.`text`
FROM `news`
CROSS APPLY (SELECT `comments`.`text`
FROM `comments`
WHERE `comments`.`newsId` = `news`.`newsId`
order by `comments`.`date` desc
limit 1) cm
order by news.date desc;
I think that what you're trying to do is:
SELECT n.newsId FROM news n
INNER JOIN comments c ON c.newsId = n.newsId
ORDER BY c.date DESC, n.date
LIMIT 1
The GROUP BY is not necessary as you are not using any aggregation function. You can have unique entries with DISTINCT
Basically, I have a coppermine gallery and I want to show the last 4 updated albums on the homepage. Here's the query that I've got so far. It basically gets the latest pictures. The subquery works fine on it's own but when it comes time to grouping them to get each album on its own, it doesn't seem to be getting the most recent one from the list.
SELECT *
FROM (
SELECT c.cid, c.name AS catname, a.aid, a.title AS albumtitle, a.category, p.aid AS albumid,p.filepath,p.filename,p.ctime AS creationtime,p.title AS pictitle,p.approved
FROM cpg145_pictures AS p LEFT JOIN `cpg145_albums` AS a ON p.aid = a.aid LEFT JOIN `cpg145_categories` AS c ON a.category = c.cid
WHERE p.approved='YES' AND a.category IN (47,48)
ORDER BY p.ctime DESC) AS T
GROUP BY albumid
ORDER BY creationtime DESC
LIMIT 4
I figured out the answer. Apparently, in MariaDB you have to give the subquery a limit for it to be sorted correctly. So:
SELECT *
FROM (
SELECT c.cid, c.name AS catname, a.aid, a.title AS albumtitle, a.category, p.aid AS albumid,p.filepath,p.filename,p.ctime AS creationtime,p.title AS pictitle,p.approved
FROM cpg145_pictures AS p LEFT JOIN `cpg145_albums` AS a ON p.aid = a.aid LEFT JOIN `cpg145_categories` AS c ON a.category = c.cid
WHERE p.approved='YES' AND a.category IN (47,48)
ORDER BY p.ctime DESC
LIMIT 200) AS T
GROUP BY albumid
ORDER BY creationtime DESC
LIMIT 4
I have this query to group together article authors on my site, along with how many articles they've done recently and their most recent posted date.
The problem is this error:
#1055 - Expression #4 of SELECT list is not in GROUP BY clause and
contains nonaggregated column 'goltest.a.author_id' which is not
functionally dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by
SELECT
COUNT(DISTINCT a.article_id) AS `counter`,
u.username,
u.user_id,
(
SELECT
`date`
FROM
`articles`
WHERE
`author_id` = a.`author_id`
ORDER BY
`article_id`
DESC
LIMIT 1
) AS `last_date`
FROM
`articles` a
LEFT JOIN
`users` u
ON
u.user_id = a.author_id
LEFT JOIN
`article_category_reference` c
ON
a.`article_id` = c.`article_id`
WHERE
a.`date` >= 1491004800 AND a.`date` <= 1495018102 AND a.`active` = 1 AND c.`category_id` NOT IN(63) AND a.author_id != 1844
GROUP BY
u.`username`,
u.`user_id`
ORDER BY
`counter`
DESC
,
a.date
DESC
Don't really understand why it wants "author_id" in the group by list, since I'm not selecting it?
For those interested I fixed it with this, replacing the subquery with MAX() value for the date:
SELECT
COUNT(DISTINCT a.article_id) AS `counter`,
u.username,
u.user_id,
MAX(a.date) AS `last_date`
FROM
`articles` a
LEFT JOIN
`users` u
ON
u.user_id = a.author_id
LEFT JOIN
`article_category_reference` c
ON
a.`article_id` = c.`article_id`
WHERE
a.`date` >= 1491004800 AND a.`date` <= 1495018102 AND a.`active` = 1 AND c.`category_id` NOT IN(63) AND a.author_id != 1844
GROUP BY
u.`username`,
u.`user_id`
ORDER BY
`counter`
DESC
,
last_date
DESC
With thanks to Shadow for pointing out "max" :)
I have wrote the query , but it is showing city id in the where clause is ambigious
SELECT
`a`.`name`,
`a`.`company`,
`a`.`city`,
`a`.`country`,
`a`.`phone`,
`a`.`type_of_enquiry`,
`b`.`enquiry_id`,
`a`.`hearaboutus`,
`a`.`email`,
`a`.`comments`,
`a`.`address`,
`c`.`id`,
`c`.`city_id`
FROM (`sobha_enquiry` a)
LEFT JOIN `sobha_enquiryzone` b
ON `b`.`enquiry_id` = `a`.`id`
LEFT JOIN `sobha_admin` c
ON `c`.`city_id` = `b`.`city_id`
WHERE `city_id` = '2'
GROUP BY `a`.`id`
ORDER BY `id` desc, `a`.`id` DESC
Pls help me !!!!!!!!!!
SELECT
`a`.`name`,
`a`.`company`,
`a`.`city`,
`a`.`country`,
`a`.`phone`,
`a`.`type_of_enquiry`,
`b`.`enquiry_id`,
`a`.`hearaboutus`,
`a`.`email`,
`a`.`comments`,
`a`.`address`,
`c`.`id`,
`c`.`city_id`
FROM (`sobha_enquiry` a)
LEFT JOIN `sobha_enquiryzone` b
ON `b`.`enquiry_id` = `a`.`id`
LEFT JOIN `sobha_admin` c
ON `c`.`city_id` = `b`.`city_id`
WHERE `a`.`city_id` = '2'
GROUP BY `a`.`id`
ORDER BY `c`.`id` desc, `a`.`id` DESC
In you where clause you should provide either b or c because are using aliases and the columns city_id exists in both table which is confusing mysql
the problem lies in the WHERE clause, you need to specify what table will be search for city_id sice both of them contains the same column name. It could be either from sobha_enquiryzone or from sobha_admin.
SELECT a.NAME,
a.company,
a.city,
a.country,
a.phone,
a.type_of_enquiry,
b.enquiry_id,
a.hearaboutus,
a.email,
a.comments,
a.address,
c.id,
c.city_id
FROM sobha_enquiry a
LEFT JOIN sobha_enquiryzone b
ON b.enquiry_id = a.id
LEFT JOIN sobha_admin c
ON c.city_id = b.city_id
WHERE b.city_id = '2' -- or c.city_id = '2' (both will yield the same result)
GROUP BY a.id
ORDER BY id DESC, a.id DESC
I have the following sample query (MySQL):
SELECT * FROM `action`
WHERE `customer_id` IN
(SELECT `id` FROM `customer` WHERE `status`=1)
ORDER BY
action.date ASC
LIMIT 0, 10
I need to be able to ORDER BY the customer.status field. Do I accomplish this with a join?
status is a field on the customer table.
Edited Query:
SELECT * FROM `action`
ORDER BY
action.date ASC
LIMIT 0, 10
IMPORTANT!
I am parsing the return data via PHP. After running the revised query:
SELECT * FROM `action` a INNER JOIN `customer` c ON a.customer_id = c.id ORDER BY a.form_id ASC LIMIT 0, 10
My PHP code breaks...
This post helped me out.
My revised query looks like this:
SELECT
*, a.id AS lead_id, c.id AS customer_id
FROM
`action` a
INNER JOIN
`customer` c ON a.customer_id = c.id
ORDER BY c.status DESC
Thanks everyone!
UPDATE
Because I have some customer records without an action record, an INNER JOIN was not returning all relevant records. I use a JOIN now, and all results come back as expected.
SELECT *
FROM `action` a
INNER JOIN `customer` c on a.`customer_id` = c.`id`
WHERE c.`status` in (1, 4, 7, 8)
ORDER BY a.date, c.status
LIMIT 0, 10
You can do either:
SELECT * FROM `action` a
INNER JOIN `customer` c on c.id = a.customer_id
WHERE c.status = 1
ORDER BY a.date ASC, c.status
LIMIT 0, 10
Or:
SELECT * FROM `action` a
INNER JOIN `customer` c on (c.id = a.customer_id and c.status = 1)
ORDER BY a.date ASC, c.status
LIMIT 0, 10
EDIT:
It's probably worth pointing out there's no sense in ordering by c.status, as it will always be 1. However, I put that in there since it was brought up by others as well as mentioned in the OP. I would think it could be removed from both queries.
Yes, you may accomplish it with a join and may be faster:
SELECT * FROM `action` a join customer c on c.id=a.customer_id
where c.status=1
ORDER BY
a.date ASC
LIMIT 0, 10
Also, consider not using * and instead list the columns that you need. It will improve performance in case you need to select less than all columns and you won't get surprises in the future if the table changes.
SELECT * FROM `action` a
JOIN `customer` c on a.customer_id=c.id
WHERE c.status=1 order by a.date, c.status ASC
LIMIT 0, 10