I have categories table:
cat_id
cat_name
Than categories - posts relationship table cat_rel:
cat_rel_id
cat_id
post_id
And posts table:
post_id
post_title
post_content
Now I have data in all these tables, I need to pull out categories without to repeat those that have more than 1 post. I get somehow category repeated if it has more than 1 post, eg. categories like:
php, python, c++, java, ruby
if I have 2 posts under PHP, I will get:
php
python
php
java
and this is the query I run:
SELECT categories.cat_name, cat_rel.post_id
FROM categories
LEFT JOIN cat_rel ON cat_rel.cat_ID = categories.cat_ID
Any help about making those categories not to repeat, would be appreciated
Use DISTINCT
Have a look here:
SQL SELECT DISTINCT Statement
This would list the categories with more than one post:
SELECT categories.cat_name as Category
, count(distinct cat_rel.post_id) as PostCount
FROM categories
LEFT JOIN cat_rel
ON cat_rel.cat_ID = categories.cat_ID
GROUP BY categories.cat_name
HAVING count(distinct cat_rel.post_id) > 1
Try this:
SELECT DISTINCT categories.cat_name, cat_rel.post_id
FROM categories
LEFT JOIN cat_rel ON cat_rel.cat_ID = categories.cat_ID
Related
I have a blog with 2 table. First posts table with a key category_id and categories table. In a page I want to show all categories with count of their posts. I write this query but it have a problem. It did not show categories with no posts. in other word categories with 0 posts did not appear in the results.
And can you help to write this in eloquent system in laravel
SELECT categories.* , Count(posts.total) as total
FROM categories
LEFT JOIN
(
SELECT * , COUNT(*) as total from posts GROUP By posts.id
) as posts
ON posts.category_id = categories.id
Try following query:
SELECT categories.* , IFNULL(Count(posts.id),0) as total
FROM categories
LEFT JOIN
posts
ON posts.category_id = categories.id
Group by categories.id;
I have little problem with SQL query in PHP.
I want to get from table post rows with category ex. 'art'.
This table don't contain name of my category (only category_id).
So, how to connect this in my query?
Tables:
post:
id, title, category_id
category:
id, name
I tried this way, but it not works.
SELECT * FROM post WHERE category_id = category.id AND category.name="art";
Anyone can help me? Thanks.
You need to join the tables.
Given your schema, this should give you an starting point:
SELECT p.* FROM post p INNER JOIN category c ON p.category_id = c.id WHERE c.name = 'art';
You are missing the JOIN
SELECT * FROM post
INNER JOIN category
WHERE category_id = category.id AND category.name="art";
You need to use join to query on name column of category table, e.g.:
SELECT p.*
FROM post p JOIN category c ON p.category_id = c.id
WHERE c.name = 'art';
I'm making a blog. I have 2 tables, one for posts and the other for categories.
I want to display the category name, category date and the number of posts in each category. I have problems to display the number of posts in each category.
In posts table I have a column called cat_id which is equal to category.id
I have these 2 MySQL queries:
mysql_query("select Count(posts.id) as NumberOfPosts, cat_id from posts group by cat_id");
And
mysql_query("select name, date from categories");
I don't know how to have combine these two queries into one query. I'm using PHP.
You could use a join:
SELECT name, date, NumberOfPosts
FROM categories c
JOIN (SELECT cat_id, COUNT(*) AS NumberOfPosts
FROM posts
GROUP BY cat_id) p ON c.id = p.cat_id
EDIT:
To include categories with no posts, you could use a left join instead of regular join. You just need to handle the nulls you'd get for NumberOfPosts, e.g., by using coalesce:
SELECT name, date, COALESCE(NumberOfPosts, 0) AS NumberOfPosts
FROM categories c
LEFT JOIN (SELECT cat_id, COUNT(*) AS NumberOfPosts
FROM posts
GROUP BY cat_id) p ON c.id = p.cat_id
I am Using PHP, MYSQL. I have two tables
Table posts with following fields
id,
title,
body,
cat_id
Table categories with following fields
cat_id,
cat_name
Suppose, I have Following Data in posts table
id = 1
title = "This is Test Post"
body = "This is the Body of Test Pots"
cat_id = 3
and in categories table
cat_id = 3
cat_name = "Mobiles"
If I Use the Following SQL Statement
"SELECT * FROM posts WHERE id=1";
It will give me following output
id = 1
title = "This is Test Post"
body = "This is the Body of Test Pots"
cat_id = 3
But I Want the Following Output
id = 1
title = "This is Test Post"
body = "This is the Body of Test Pots"
cat_id = Mobiles
How can I get the above mentioned output.
Note: I know some kind of JOIN is used in this kind of situation, But I don't know how to use it. One More thing, Is it possible to get my desired output without using any JOIN, because I heard that JOINs Effect the Efficiency. If JOIN is necessary Please tell me the most efficient Way to get my desired output.
SELECT * FROM posts JOIN categories USING (cat_id) WHERE posts.id = 1
It's possible to achieve the same using a correlated subquery instead (however this is likely to be less efficient than a join):
SELECT *, (SELECT cat_name FROM categories WHERE cat_id = posts.cat_id)
FROM posts
WHERE id = 1
You have to use join querty to join posts and categories in order to get informations from both tables. So you try with the following query
SELECT t1.*,t2.cat_name FROM posts as t1 join categories as t2 on t1.cat_id = t2.cat_id WHERE t1.id=1
Use join query
try this
SELECT id, title, body, cat_name FROM posts
join categories on posts.cat_id = categories.cat_id WHERE id=1
SELECT post.id, post.title, post.body, cat.cat_name from posts inner
join categories cat on cat.cat_id = post.cat_id
and post.id = 1
I think you have to learn JOINS first read this articles
JOINS
JOINS VS SUB-QUERY
now about your question checkout the
SQL FIDDLE
SELECT * FROM posts As p
JOIN categories AS c ON c.id = p.cat_id
WHERE p.id = 1
now its your choice whether to use Joins or Sub-Queries both have its pros and cons
thus select as per your requirement.
hope this will help you ...!
What im trying to do was to list all categories and their posts but only limit posts per category. And exclude a category without any posts.
I did this with two queries though, get all the categories that have posts, loop the results and get X number of posts per category ID.
How can I do this in just 1 query?
EDIT: this is what I accomplished so far..
SELECT p.post_id, c.category_id
FROM category as c
JOIN posts AS p ON p.category_id = c.category_id
WHERE
FIND_IN_SET(p.post_id, (
SELECT SUBSTRING_INDEX(a.post_ids, ',', 10)
FROM
(
SELECT GROUP_CONCAT(b.post_id) AS post_ids, b.category_id
FROM posts as b
GROUP BY b.category_id
) AS a
WHERE a.category_id = c.category_id
))
To exclude all categories without any posts do an inner join between category and post. If you want to limit the number of rows returned, use the LIMIT command.
how about something like this
SELECT `category_name`
FROM `categories`
WHERE `posts` !=0
LIMIT 0 , 30