City_id in the where clause is ambiguous - mysql

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

Related

MySql Query Order BY datetime and Group By Id issue

Having issue with this query
there is 3 rows in comments table with different date-times, I want customer list with last comment created_date / updated_date.
but didn't getting last commented customer with group by customer
SELECT * FROM(
SELECT MAX(comments.`date_updated`), customer.id AS vid, comments.`date_updated` AS dts, comments.`id` AS comments_id, comments.* FROM customer
INNER JOIN comments ON comments.`customer_id` = customer.`id`
WHERE customer.`id` IN ('')
) AS v
GROUP BY v.`vid` LIMIT 0,50
You use a self join to comments table and filter row for each customer with latest date_updated
SELECT c.id AS vid, co.`date_updated` AS dts, co.`id` AS comments_id, co.*
FROM customer c
INNER JOIN comments co ON co.`customer_id` = c.`id`
LEFT JOIN comments co1 ON co.`customer_id` = co1.`customer_id` AND co.date_updated < co1.date_updated
WHERE co1.customer_id IS NULL AND c.`id` IN ('')
Or with inner join
SELECT c.id AS vid, co.`date_updated` AS dts, co.`id` AS comments_id, co.*
FROM customer c
INNER JOIN comments co ON co.`customer_id` = c.`id`
INNER JOIN (
SELECT customer_id, MAX(date_updated) date_updated
FROM comments
GROUP BY customer_id
) co1 ON co.customer_id = co1.customer_id AND co.date_updated = co1.date_updated
WHERE c.`id` IN ('')
You forgot order by clause
SELECT * FROM(
SELECT MAX(comments.`date_updated`), customer.id AS vid, comments.`date_updated` AS dts, comments.`id` AS comments_id, comments.* FROM customer
INNER JOIN comments ON comments.`customer_id` = customer.`id`
WHERE customer.`id` IN ('')
) AS v
GROUP BY v.`vid` LIMIT 0,50
ORDER BY created_date desc;

MySQL query very slow on group by and count

I have a database with the following tables:
hospitals (~28K rows),
inspections (~116K rows),
issues (~290K rows)
Hospitals have inspections and each inspection has zero or more issues. I have the following query:
SELECT count(*) as count, BName, BCity, BAddress, BState, BZip, Ins_date, BCountry, Ins_Type
FROM ( SELECT b.id as ID, b.hospital_name as BName, b.city as BCity, b.address as BAddress, b.country as BCountry, b.state as BState, b.zip as
BPostal, i.date as Ins_date, v.type as Ins_Type
FROM hospital_table b, inspection_table i, issue_table v
WHERE b.id = i.business_id
AND i.id = v.inspection_id
ORDER BY b.hospital_name, i.date DESC ) AS sumissues
GROUP BY ID
ORDER BY count DESC;
The output I expect and get is:
112 | Burnaby Memorial | Burnaby | 3935 Kincaid St | BC | 2017-07-19 | Canada | Cleanliness
The problem is it takes about 40seconds to run. I have an index on the foreign keys, and inspection_table.date. Any ideas on how I can optimize this?
Looking to your code
The order by in subselect not needed and also the subselect is not needed
SELECT
count(*) as count
, b.hospital_name as BName
, b.city as BCity
, b.address as BAddress
, b.country as BCountry
, b.state as BState
, b.zip as BPostal
, i.date as Ins_date
, v.type as Ins_Type
FROM hospital_table b
INNER JOIN inspection_table i ON b.id = i.business_id
INNER JOIN issue_table v ON i.id = v.inspection_id
GROUP BY b.id
ORDER BY count DESC, b.hospital_name, i.date
But be careful for what in commente by #User_by_Already
for this and for avoi more rows group for the others column and if you don'care for the result of these columns you can use (fake) aggregation function
SELECT
count(*) as count
, b.hospital_name as BName
, b.city as BCity
, b.address as BAddress
, b.country as BCountry
, b.state as BState
, b.zip as BPostal
, min(i.date) as Ins_date
, min(v.type) as Ins_Type
FROM hospital_table b
INNER JOIN inspection_table i ON b.id = i.business_id
INNER JOIN issue_table v ON i.id = v.inspection_id
GROUP BY b.id
ORDER BY count DESC, b.hospital_name, i.date

MySQL: Sorting results before group by statement

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

MySQL random join

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!

mysql, use two select and order by, new value

SELECT a.id, a.name, a.ad, c.name, c.phone, c.email,
(
SELECT b.id_user
FROM price_b b
WHERE b.id = a.id
ORDER BY b.date DESC
LIMIT 1
) AS f_user_id
FROM a_emtp a
LEFT JOIN customer c ON
c.id = f_user_id
WHERE a.show = "1"
Hi, why show this error: Unknown column 'f_user_id' in 'on clause'
Thanks
SELECT a.id, a.name, a.ad, c.name, c.phone, c.email,
(
SELECT b.id_user
as f_user_id
FROM price_b b
WHERE b.id = a.id
ORDER BY b.date DESC
LIMIT 1
)
FROM a_emtp a
LEFT JOIN customer c ON
c.id = f_user_id
WHERE a.show = "1"
You can reference labelled fields from subqueries inside outer queries. You could even drop the relabelling and just use "b.id_user".
Probably a comma after c.email was missing.
SELECT a.id, a.name, a.ad, c.name, c.phone, c.email,
(
SELECT b.id_user
FROM price_b b
WHERE b.id = a.id
ORDER BY b.date DESC
LIMIT 1
) AS f_user_id
FROM a_emtp a
LEFT JOIN customer c ON
c.id = f_user_id
WHERE a.show = "1"