How to join twice times to another table? - php

I got this code:
SELECT
topics.id,
topics.id_first,
posts.id_first
FROM topics
LEFT
JOIN posts
ON posts.id = topics.id_first_msg
My intention, is to do something like this:
SELECT
topics.id,
topics.id_first,
posts.id_first,
posts.id_last
FROM topics
LEFT
JOIN posts
ON posts.id = topics.id_first_msg
LEFT
JOIN posts
ON posts.id = topics.id_last_msg
But, when I try to do Left Join twice, I get an error. Which is the correct way then? Thanks.

You need to provide aliases for the table you're joining more than once:
SELECT
topics.id,
topics.id_first,
p1.id_first,
p2.id_last
FROM topics
LEFT JOIN posts p1 ON p1.id = topics.id_first_msg
LEFT JOIN posts p2 ON p2.id = topics.id_last_msg

You have to alias the second left join;
SELECT
topics.id,
topics.id_first,
posts.id_first,
posts2.id_last
FROM topics
LEFT
JOIN posts
ON posts.id = topics.id_first_msg
LEFT
JOIN posts AS posts2
ON posts2.id = topics.id_last_msg

The posts table is ambiguous.
First off - do you really need to join twice? Can't you just do this:
SELECT
topics.id,
topics.id_first,
posts.id_first,
posts.id_last,
posts.id_first,
posts.id_last
FROM topics
LEFT
JOIN posts
ON posts.id = topics.id_first_msg
Second - if you really have to join twice - give the posts table two different aliases (you'll still need to edit the selected columns)

Related

MySQLi inner join not working properly

I have a query that select shows alongside with it's rating. But it will not work if there isn't any rates. I want it to work even when it finds zero results on the rating table.
My query is
$shows = $DB->query('SELECT
p.id, p.title, p.cover, p.summary, p.genre, p.year,
ROUND(AVG(pr.rating), 1) AS rating_average
FROM shows p
INNER JOIN shows_ratings pr
ON pr.showid = p.id');
Change inner join with left join.
Explanation: You are trying to join two table on key which doesn't exist in other table, that's why you are not getting any result in inner join. Whereas left join will return rating as empty when key is not present in rating table.
$shows = $DB->query('SELECT
p.id, p.title, p.cover, p.summary, p.genre, p.year,
ROUND(AVG(pr.rating), 1) AS rating_average
FROM shows p
LEFT JOIN shows_ratings pr
ON pr.showid = p.id');

Group by and count data with one query

This is my table structure in database
I want to get number of likes for each post by counting post field in likes table and display it for every post using foreach loop.
My question is
Is there a way i can do this with one query with JOIN, GROUP BY and COUNT tables and not create multiple queries.
select p.id, p.title, p.content, count(l.id) as likes_count
from posts p
left join likes l on l.post = p.id
group by p.id, p.title, p.content
http://sqlfiddle.com/#!9/bca8ee/1
SELECT p.*, COUNT(l.id)
FROM posts p
LEFT JOIN likes l
ON l.post = p.id
GROUP BY p.id

how to order data in mysql by join count?

I have two tables :
posts : id,title,content,show,created_at
comments: id,post_id,created_at
I'm trying to order posts by most commented.
SELECT *, COUNT(comments.id) AS total_comments
FROM comments LEFT JOIN posts ON posts.id = comments.post_id
WHERE posts.show = '1'
GROUP BY complains.id
ORDER BY total_comments DESC
The problem is that the posts with 0 comments don't appear.
Any help would be much appreciated.
With your join above, you are incorrectly joining to get commens that have posts
You should have done a right join or swap the tables in left join like below.
Select *, COUNT(comments.id) as total_comments
FROM posts
LEFT outer JOIN comments on posts.id = comments.post_id
WHERE posts.show = '1'
GROUP BY posts.id
ORDER BY total_comments DESC
You need to do a RIGHT JOIN instead of a LEFT JOIN. Or swap the tables in the LEFT JOIN clause.
While there are many ways to solve this, I think this code is easy to read and understand.
The query in the LEFT JOIN can be copied out and run on its own to help debug. Then you join that result set to the posts table and order the results.
SELECT p.*, IFNULL(c.total_comments, 0) as total_comments
FROM posts p
LEFT JOIN (select post_id, count(post_id) as total_comments from comments group by post_id) as c ON p.id = c.post_id
WHERE p.show = '1'
ORDER BY c.total_comments DESC

Count number of articles in categories

This code only shows categories with articles in them.
I want to show all categories.
Pls help.
$query = "SELECT C.id, C.jcat_name,COUNT(A.catid) AS catid FROM jt_categories C INNER JOIN jt_articles A ON C.id = A.catid GROUP BY C.id";
change to left join
SELECT C.id, C.jcat_name,COUNT(A.catid) AS catid FROM jt_categories C
LEFT JOIN jt_articles A ON C.id = A.catid GROUP BY C.id
Change INNER JOIN for LEFT JOIN in your query.
INNER JOIN looks explicitely for the join in the data
replace the inner join by a left outer join
Change INNER JOIN to LEFT JOIN.
Did u try LEFT JOIN ? bc. (i think) in second table u have NULL articles for some categories.

How to query 3 tables in a single query?

I'm really sorry for the first post as i didn't explain everything.
Basically i have 3 tables, One for posts, One for Categories, & Another to link categories with posts.
I want in a single MySQL query to select posts that are under a specific category.
posts(id,title,body)
---------------------
125,Some title,Blah blah
categories(id,name)
---------------------
1,politic
2,entertainment
linker(categoryid,postid)
---------------------
2,125
I want in single query to fetch posts in the entertainment category by example, what to do?
Thanks
select
p.*
from
posts p
inner join linker l on l.postid = p.id
inner join categories c on c.categoryid = l.categoryid
where
c.name = 'entertainment'
The following SQL statement should provide you with a basis for what you are trying to do.
select p.*
from posts p
inner join linker l
on l.postid = p.id
inner join categories c
on l.categoryid = c.id
where c.name = 'entertainment'
If a post belongs to 2 categories, you can still use pinkfloydx33's query with DISTINCT in the select statement:
select
DISTINCT p.*
from
posts p
inner join linker l on l.postid = p.id
inner join categories c on c.categoryid = l.categoryid
where
c.name = 'entertainment'
The result set will show only one record.
It's the same exact thing, you just have to join 3 tables intead of 2 :
SELECT P.id post_id,
P.title,
P.body,
C.id category_id,
C.name
FROM posts P
INNER JOIN linker L
ON P.id = L.postid
INNER JOIN categories C
ON L.categoryid = C.id
WHERE C.name = 'Category'
Don't be afraid to do your own tests. If you understand how to join two tables, you should understand how to join three, four and more.
If you are specifying only one category in the WHERE clause, then the result will be a single row for each post ID.
Either way you can use DISTINCT or GROUP BY when the result could be more than one row per ID, but in that case i prefer the second one (GROUP BY).

Categories