How can i select all the rows with a count>0 ?
"where hits>0" does not work.
SELECT b . * , COUNT( a.abfrage_id ) AS `hits`
FROM abfragen_ergebnisse a
RIGHT JOIN suchen b ON a.abfrage_id = b.id
WHERE b.id <140
GROUP BY b.id
ORDER BY `hits` DESC , b.`id` ASC
You should use HAVING:
SELECT b . * , COUNT( a.abfrage_id ) AS `hits`
FROM abfragen_ergebnisse a
RIGHT JOIN suchen b ON a.abfrage_id = b.id
WHERE b.id <140
GROUP BY b.id
HAVING `hits` > 0
ORDER BY `hits` DESC , b.`id` ASC
Related
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
so i had to adjust a query i used because the limit at the end of the table would cause the entire first table to be read before limiting. this resulted in a timeout from my mysql server. now i created the query like i read in another post on stack overflow and came up with this:
SELECT a.title
, a.lat
, a.lon
, a.a_content_id
, a.date_added
, r.countRep
, i.countInt
, content.img
, c.marker
, c.subcatvan
FROM
( SELECT title
, lat
, lon
, alert_content_id
, date_added
, cat
FROM alerts
LIMIT 10
) a
LEFT
JOIN
( SELECT COUNT(DISTINCT id) countRep
FROM reply
WHERE alert_id = alerts.alerts
) r
LEFT
JOIN
( SELECT COUNT(DISTINCT id) countInt
FROM interactions
WHERE alert_id = alerts.alerts
) i
LEFT
JOIN
( SELECT img
FROM alerts_content
WHERE alert_id = alerts.alerts
) content
LEFT
JOIN
( SELECT marker
, subcatvan
FROM categories
WHERE a.cat = id
) c;
#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 '' at line 45
This was my original query which resulted in a timeout:
SELECT
a.title, a.lat, a.lon, a.alert_content_id, a.date_added, count(DISTINCT r.id) as countRep ,count(DISTINCT i.id) AS countInt,
ac.img, c.marker, c.subcatvan
FROM `alerts` a
LEFT JOIN
`reply` r ON r.alert_id = a.alerts
LEFT JOIN
`interactions` i ON i.alert_id = a.alerts
LEFT JOIN
`alerts_content` ac ON ac.alert_id = a.alerts
LEFT JOIN
`categories` c ON a.cat = c.id
GROUP BY a.title, a.lat, a.lon, a.alert_content_id, a.date_added LIMIT 0,10
Does anyone know what is causing the error?
Or someone that knows how to correct my original query?
You need to fix the subqueries. They need to use group by rather than be correlated:
SELECT a.title, a.lat, a.lon, a.a_content_id, a.date_added, r.countRep,
i.countInt, content.img, c.marker, c.subcatvan
FROM (SELECT title, lat, lon, alert_content_id, date_added, cat
FROM `alerts`
LIMIT 10
) a LEFT JOIN
(SELECT alert_id, count(DISTINCT id) as countRep
FROM `reply`
GROUP BY alert_id
) r
ON r.alert_id = a.alerts r LEFT JOIN
(SELECT alert_id, count(DISTINCT id) AS countInt
FROM `interactions`
GROUP BY alert_id
) i
ON i alert_id = a.alerts LEFT JOIN
(SELECT alert_id, img
FROM `alerts_content`
GROUP BY alert_id
) content
ON alert_id = a.alerts LEFT JOIN
`categories` c
ON a.cat = c.id;
Try adding the table name to the id, in the where.
c.id instead of id
Fixed it thanks to #Ravinder his comment about missing the on clause
updated sql:
SELECT
a.alerts, a.title, a.lat, a.lon, a.alert_content_id, a.date_added, r.countRep, i.countInt,
ac.img, c.marker, c.subcatvan
FROM
(SELECT alerts, title, lat, lon, alert_content_id, date_added, cat
FROM `alerts` LIMIT 10) a
LEFT JOIN
(SELECT alert_id,count(DISTINCT id) as countRep
FROM `reply`) r
ON r.alert_id = a.alerts
LEFT JOIN
(SELECT alert_id,count(DISTINCT id) AS countInt
FROM `interactions`) i
ON i.alert_id = a.alerts
LEFT JOIN
(SELECT alert_id, img
FROM `alerts_content`) ac
ON ac.alert_id = a.alerts
LEFT JOIN
(SELECT id, marker, subcatvan
FROM `categories`) c
ON a.cat = c.id
GROUP BY a.title, a.lat, a.lon, a.alert_content_id, a.date_added
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 this query which is working fine:
SELECT c.id, c.name
FROM job j
LEFT JOIN portfolio_job_category pjc ON pjc.job_id = j.id
LEFT JOIN category c ON c.id = pjc.category_id
WHERE j.end_date >= DATE( NOW( ) )
AND pjc.portfolio_id =3
GROUP BY c.id
ORDER BY `c`.`id` ASC
LIMIT 0 , 30
But when I do query below it doesn't work and returns empty rows:
SELECT c.id, c.name
FROM commstratjobs.job j
LEFT JOIN commstratjobs.portfolio_job_category pjc ON pjc.job_id = j.id
LEFT JOIN commstratjobs.category c ON c.id = pjc.category_id
WHERE j.end_date >= DATE( NOW( ) )
AND pjc.portfolio_id =3
GROUP BY c.id
ORDER BY c.name;
LIMIT 0 , 30
Is there a reason why?
Only thing I did was to add database in front of tables...
did you try to seprate database & table by this symbol : `
i.e
commstratjobs.job
so your query will be like this
SELECT c.id, c.name
FROM `commstratjobs`.`job j`
LEFT JOIN `commstratjobs`.`portfolio_job_category` pjc ON pjc.job_id = j.id
LEFT JOIN `commstratjobs`.`category` c ON c.id = pjc.category_id
WHERE j.end_date >= DATE( NOW( ) )
AND pjc.portfolio_id =3
GROUP BY c.id
ORDER BY c.name;
LIMIT 0 , 30
SELECT DISTINCT (
A.`id`
), A.`id` , A.`no` , B.amount, SUM( A.`outward` * A.`price` ) AS total, A.`outward_date`
FROM `outward` A
INNER JOIN franchisees B
INNER JOIN store C
INNER JOIN shoppe D
WHERE B.user_id = C.user_id
AND (C.pos_id = A.no)//(C.pos_id = A.no OR (D.id = A.no)
OR (D.id = A.no)
AND A.outward_date = '2012-02-10'
GROUP BY A.req_id
ORDER BY A.no ASC , A.`d` ASC , B.amount ASC
The problem in this query is that the SUM( A.outward * A.price ) AS total comes differently which is not related to
ouptut
id sum(outward * price)
12021030738-105 485.220000000000
1202104186-104 2504.410000000000
output displayed
12021030738-105 32557.33
1202104186-104 6307.86
i guess the problem is with the OR statement? can anyone find the issue with the query
Try below. enclose OR Conditions in a parenthesis:
SELECT DISTINCT (
A.`id`
), A.`id` , A.`no` , B.amount, SUM( A.`outward` * A.`price` ) AS total, A.`outward_date`
FROM `outward` A
INNER JOIN franchisees B
INNER JOIN store C
INNER JOIN shoppe D
WHERE B.user_id = C.user_id
AND ((C.pos_id = A.no) OR (D.id = A.no))
AND A.outward_date = '2012-02-10'
GROUP BY A.req_id
ORDER BY A.no ASC , A.`d` ASC , B.amount ASC