MySQL query not working - can't figure out why - mysql

I have this query that I'm pretty sure it set up correctly, but it keeps returning no results, when I know there is 1 row that should be returned.
SELECT
`t1`.id,
`t1`.length,
`t2`.last,
`t2`.first,
`t4`.name,
`t5`.text
FROM `res` AS `t1`
INNER JOIN `pt` AS `t2`
ON `t1`.ptid=`t2`.pt
INNER JOIN `docs` AS `t3`
ON `t1`.doc=`t3`.did
LEFT JOIN `user` AS `t4`
ON `t3`.user=`t4`.id
INNER JOIN `desc` AS `t5`
ON `t1`.desc=`t5`.id
ORDER BY
`t1`.date ASC
Again, not getting an error - just getting no results.
Thanks

desc is a reserved word. You need to put backticks around it.
JOIN `desc` as `t5` ON `t1`.desc=`t5`.id
should be
JOIN `desc` as `t5` ON `t1`.`desc`=`t5`.id

Related

Include zero in COUNT with GROUP BY in MySQL

I'm looking to make a query in MySQL to list all the faculties and their number of students given the following table structure:
My query looks like:
SELECT `f`.`id`, `f`.`name`, COUNT(*) `total`
FROM `student` `s`
INNER JOIN `course` `c` ON `c`.`id` = `s`.`course_id`
LEFT JOIN `faculty` `f` ON `f`.`id` = `c`.`faculty_id`
GROUP BY `c`.`faculty_id`
ORDER BY `f`.`name`
And I'm getting this result:
but I need to get all the faculties, even the ones without registered students.
If I use a LEFT JOIN with the course table I get the same results.
If you want all the faculties; your starting table for the JOIN should be the faculty table. Then do Left joins on the other table accordingly.
Use the following query:
SELECT `f`.`id`, `f`.`name`, COUNT(`s`.`id`) AS `total`
FROM `faculty` AS `f`
LEFT JOIN `course` AS `c` ON `f`.`id` = `c`.`faculty_id`
LEFT JOIN `student` AS `s` ON `c`.`id` = `s`.`course_id`
GROUP BY `f`.`id`, `f`.`name`
ORDER BY `f`.`name`
You need to revese the query, your primary tables is faculties (or use right join) :
important - since you want to count cases of zero students you need to treat NULL values that might come up, joining the students table:
SELECT
`f`.`id`, `f`.`name`, SUM(IF(s.id IS NULL, 0,1) `total`
FROM
faculty f
LEFT JOIN course c ON `f`.`id` = `c`.`faculty_id`
LEFT JOIN student s ON `c`.`id` = `s`.`course_id`
GROUP BY `c`.`faculty_id` ORDER BY `f`.`name`

MYSQL GROUP BY - slow query

I have a query like below, please help to give proper index for this, the contact table have more than 20K records and it take nearly 20 secs to load.
Hope the group by clause makes the problem, if I remove the group by clause total record is more than 300k.
SELECT `a`.*, CONCAT(a.`firstname`, " ", a.`lastname`) AS `cont_name`, CONCAT(a.`position`, " / ", a.`company`) AS `comp_pos`, `e`.`name` AS `industry_name`, CONCAT(f.`firstname`, " ", f.`lastname`) AS `created_by`
FROM `contacts` AS `a`
LEFT JOIN `user_centres` AS `b` ON a.user_id = b.user_id
LEFT JOIN `group_contacts` AS `c` ON a.id = c.contact_id
LEFT JOIN `groups` AS `d` ON c.group_id = d.id
LEFT JOIN `industries` AS `e` ON e.id = a.industry_id
LEFT JOIN `users` AS `f` ON f.id = a.user_id
WHERE (1)
GROUP BY `a`.`id`
ORDER BY `a`.`created` desc
Explain shows like this - 20145 Using temporary; Using filesort
You can try these steps
There is no use of table group ( d ), so you can remove left join to group from this query
Add index for user_id, contact_id and industry_id ( I hope these ids are joining table primary_keys ) in contacts table
check these ids user_id, contact_id and industry_id types ( INT ) are same.

SQL Inner Join issues

Currently I have the following command:
SELECT * FROM `clients`
WHERE `ID`=(SELECT `clientID`
FROM `websites`
WHERE `name` LIKE "%my name%")
LIMIT 0,20
I wish to use an inner join, because AFAIK that is how it should be handled.
I have no idea how to do it... Here is all I could think of:
SELECT *
FROM `clients`
WHERE INNER JOIN `websites` ON `websites`.`name` LIKE "%lead%"
LIMIT 0,20
SELECT c.* FROM `clients` c
INNER JOIN `websites` w ON w.clientID = c.ID
WHERE w.`name` LIKE '%lead%'
LIMIT 0,20

MySQL Query Output

I am trying to run this query however it's only outputting results of categories that have items in them, rather than all the categories. This is my query:
SELECT categories.`id` as `cat_id`, categories.`title` as `cat_title`, COUNT(tutorials.`id`) as `total`
FROM `tutorial_categories` as `categories`, `tutorials`
WHERE tutorials.`category` = categories.id
GROUP BY `cat_id`
Can anyone lead me in the right direction here? Thanks.
SELECT
categories.id ,
categories.title,
COUNT(*) as total
FROM tutorial_categories as categories
LEFT JOIN tutorials
on tutorials.category = categories.id
GROUP BY categories.id
You need to use a LEFT JOIN. Your implicit join syntax produces an inner join insetad:
SELECT
categories.`id` as `cat_id`,
categories.`title` as `cat_title`,
COUNT(tutorials.`id`) as `total`
FROM
`tutorial_categories` `categories` LEFT JOIN `tutorials` ON categories.id = tutorials.category
GROUP BY `cat_id`
You are looking for an outer join as described here : "An outer join does not require each record in the two joined tables to have a matching record"
You need to restyle the query using an outer join, instead of using the WHERE
Something like this
SELECT categories.`id` as `cat_id`, categories.`title` as `cat_title`, COUNT(tutorials.`id`) as `total`
FROM `tutorial_categories` as `categories`, `tutorials`
LEFT OUTER JOIN tutorials.`category` = categories.id
GROUP BY `cat_id`
You have a WHERE tutorials.category= categories.id set so it would only return the rows that meet that criteria. Remove that and it should return "all the categories".
SELECT categories.`id` as `cat_id`, categories.`title` as `cat_title`, COUNT(tutorials.`id`) as `total`
FROM `tutorial_categories` as `categories`, `tutorials`
GROUP BY `cat_id`

Inner Join in ON Clause

I have a MySQL query that is trying to get all the Pages that contain Data like 'word%'. I have a many-to-many table called Pages2Data. It seems that to do this, I need to have an inner join connecting the Pages to the Pages2Data table, and then another inner join connecting Pages2Data to Data.
The following did not work, because the nested SELECT clause can return more than one row. I'm not sure how to fix it though:
SELECT * FROM `Pages`
INNER JOIN `Pages2Data` ON
(`Pages2Data`.`DataID`=(SELECT `DataID` FROM `Data` WHERE `DataWord` LIKE 'word%'))
AND `Pages`.`PageID`=`Pages2Data`.`PageID`;
SELECT * FROM `Pages`
INNER JOIN `Pages2Data`
ON `Pages`.`PageID`=`Pages2Data`.`PageID`
INNER JOIN `Data` ON `Data`.`DataID`= `Pages2Data`.`DataID`
WHERE `DataWord` LIKE 'word%';
First the problems with your query:
the join condition should be next to the ON clause
there is no WHERE clause in the outer most query
Fixed query:
SELECT * FROM `Pages`
INNER JOIN `Pages2Data` ON `Pages`.`PageID`=`Pages2Data`.`PageID`
WHERE `Pages2Data`.`DataID`= (SELECT `DataID` FROM `Data` WHERE `DataWord` LIKE 'word%');
Alternative query:
SELECT `PG`.*
FROM `Pages` `PG`
INNER JOIN `Pages2Data` `PD` ON `PD`.`PageID` = `PG`.`PageID`
INNER JOIN `Data` `DA` ON `PD`.`DataID` = `DA`.`DataID`
WHERE `DA`.`DataWord` LIKE 'word%';
try this
SELECT * FROM `Pages` p, `Pages2Data` p2d, `Data` d
WHERE p.`PageID` = p2d.`PageID`
AND p2d.`DataID` = d.`DataID`
AND `DataWord` LIKE 'word%'
You can check 'Like' in where clause.
SELECT * FROM
`Pages`
INNER JOIN
`Pages2Data` ON (`Pages`.`PageID`=`Pages2Data`.`PageID`)
WHERE
`DataWord` LIKE 'word%'
SELECT *
FROM Pages
INNER JOIN Pages2Data
ON Pages.PageID = Pages2Data.PageID
INNER JOIN Data
ON Pages2Data.DataID = Data.DataID
WHERE DataWord LIKE 'word%'