MySQL Query Output - mysql

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`

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 - Ordering IN within INNER JOIN

I'm creating a system that allows a user to search a database of photo albums images for a keyword, it's working great, the only issue is that I'm ordering relevancy by the amount of times that keyword appears in an album. I'm doing this using:
SELECT collections_ids.collection_id
FROM `keywords`
INNER JOIN collections_ids ON keywords.id = collections_ids.photo_id
WHERE keywords.`keyword` = 'trees'
GROUP BY collection_id
ORDER BY COUNT(*) DESC
As said, this works great.
The only issue is, when this is included in an "WHERE IN" query, it loses it's order and is returned randomly. For clarity, here is the query:
SELECT collections.id,
collections.title
images.img_small
FROM `collections`
INNER JOIN images ON images.id = collections.cover_photo
WHERE collections.`id` IN
(SELECT collections_ids.collection_id
FROM `keywords`
INNER JOIN collections_ids ON keywords.id = collections_ids.photo_id
WHERE keywords.`keyword` = 'trees'
GROUP BY collection_id
ORDER BY COUNT(*) DESC)
I've tried researching, and people have suggested using the FIELD function, but I don't see that working in this context.
Any suggestions?
you can use sub query as join and take it count(*) as order by like below
SELECT collections.id,
collections.title
images.img_small
FROM `collections`
INNER JOIN images ON images.id = collections.cover_photo
INNER JOIN
(SELECT distinct collections_ids.collection_id As collection_id,COUNT(*) as total
FROM `keywords`
INNER JOIN collections_ids ON keywords.id = collections_ids.photo_id
WHERE keywords.`keyword` = 'trees'
GROUP BY collection_id
) as A
ON A.collection_id =collections.collection_id
order by A.total

Select MySQL Month() for null column

Got a question to ask. How do i get to list all of my left table to display the data when i use the left join even when some row got null value in it?
Heres my query:
SELECT *, SUM(transactions.debit_credit) current_spending
FROM (`budget`) LEFT JOIN `categories` ON `budget`.`category`=`categories`.`idcategory`
LEFT JOIN `user_category` ON `budget`.`category`=`user_category`.`idcategory`
LEFT JOIN `category_transaction` ON `budget`.`category`=`category_transaction`.`idcategory`
LEFT JOIN `transactions` ON `category_transaction`.`idtransaction`=`transactions`.`idtransaction`
WHERE `user_category`.`iduser` = '1'
AND MONTH(transactions.transaction_date) = '11'
GROUP BY `budget`.`idbudget`;
When I remove AND MONTH(transactions.transaction_date) = '11' the query works fine accept it displays all data instead of just 1 month of transaction.
Help?? Please?? Any idea how??
Your where clause turns your left join into an inner join because you filter the data on a condition of a joined table.
Instead put that condition in the on clause of your join
SELECT *, SUM(transactions.debit_credit) current_spending
FROM (`budget`)
LEFT JOIN `categories` ON `budget`.`category`=`categories`.`idcategory`
LEFT JOIN `user_category` ON `budget`.`category`=`user_category`.`idcategory`
AND `user_category`.`iduser` = '1'
LEFT JOIN `category_transaction` ON `budget`.`category`=`category_transaction`.`idcategory`
LEFT JOIN `transactions` ON `category_transaction`.`idtransaction`=`transactions`.`idtransaction`
AND MONTH(transactions.transaction_date) = '11'
GROUP BY `budget`.`idbudget`;

mysql inner join statement error

I am getting the following error:
1052 - Column 'product_id' in field list is ambiguous
When I run the following:
SELECT `product_id`, `product_name`
FROM `products`
INNER JOIN `products_has_product_category`
ON `products.product_id` = `products_has_product_category.product_id`
AND `products_has_product_category.category_id` = 1
ORDER BY `products.product_name`
My PRODUCTS table has
product_id, product_name, etc
My products_has_product_category table has
product_id, category_id
This is my first try at a join, so I appreciate the help!
You need to specify which table the product_id comes from. Since the product_id is in both tables, when you SELECT it you need to specify which table you want the value from. With a table alias:
SELECT p.product_id, p.product_name
FROM `products` p
INNER JOIN `products_has_product_category` pc
ON p.product_id = pc.product_id
AND pc.category_id = 1
ORDER BY p.product_name
Without table aliases:
SELECT `products`.`product_id`, `products`.`product_name`
FROM `products`
INNER JOIN `products_has_product_category`
ON `products.product_id` = `products_has_product_category.product_id`
AND `products_has_product_category.category_id` = 1
ORDER BY `products.product_name`
If I understand your intention correctly, you probably meant WHERE rather than AND:
SELECT `products`.`product_id`, `products`.`product_name`
FROM `products`
INNER JOIN `products_has_product_category`
ON `products.product_id` = `products_has_product_category.product_id`
WHERE `products_has_product_category.category_id` = 1
ORDER BY `products.product_name`

Mysql joins problem

I use this query to select all articles :
SELECT articles.*,categories.category_name,users.username,tags.tag
FROM articles
LEFT JOIN `categories` ON articles.category_id = categories.category_id
LEFT JOIN `users` ON articles.author_id = users.user_id
LEFT JOIN `tags` ON articles.article_id = tags.article_id
ORDER BY articles.date_added DESC
I have an other table comments, and I want to count how many comments are there, where the article_id in that table = article_id in the articles table. I tried with COUNT, but then it returns only one result. How can I do that with one query?
You can use a subquery in the SELECT clause:
SELECT articles.*,categories.category_name,users.username,tags.tag, (SELECT count(*) FROM comments c WHERE c.article_id = articles.article_id) as comments_count
As arnaud576875 already stated, you can use a subquery to extract the summary data.
Two things I've noticed from your SQL that are not really a part of the question but still worth pointing out.
you can use a table alias to shorten your SQL and make it more readable.
So instead of
SELECT articles.*,categories.category_name,users.username,tags.tag
FROM articles
LEFT JOIN `categories` ON articles.category_id = categories.category_id
LEFT JOIN `users` ON articles.author_id = users.user_id
LEFT JOIN `tags` ON articles.article_id = tags.article_id
ORDER BY articles.date_added DESC
you'd code
SELECT a.*, c.category_name, u.username, t.tag
FROM articles a
LEFT JOIN `categories` c ON a.category_id = c.category_id
LEFT JOIN `users` u ON a.author_id = u.user_id
LEFT JOIN `tags` t ON a.article_id = t.article_id
ORDER BY a.date_added DESC
I would drop SELECT * and select only the fields that you actually are going to use. This also helps with readability of your code.