MySQL How to get two results with one query from different tables - php

I have the following tables:
table name -----| table fields
books -------------| book_id, book_title
authors ---------- | author_id, author_name
books_authors - | author_id, book_id
users ------------- | user_id, user_name
comments -------- | comment_id, comment
comments_users | comment_id, user_id, book_id, date
I need to show the book title, its authors, and all the comments posted for this book (I need to show user name, date, comment).
I make do it with two different sql queries, but I don't know how to do it with just 1 query.
Here are the two queries:
SELECT * FROM books
INNER JOIN books_authors
ON books.book_id=books_authors.book_id
INNER JOIN authors
ON books_authors.author_id=authors.author_id
WHERE books.book_id=7
this one returns the name and authors of book with id=7.
SELECT `user_name`, `comment`, `date`
FROM comments_users
INNER JOIN comments ON comments_users.comment_id = comments.comment_id
INNER JOIN users ON comments_users.user_id = users.user_id
WHERE comments_users.book_id=7
this one returns the user name, comment, and the date the comment was posted.
Can anyone explain me how can I join them into one query? Thanks in advance

This arranged query should suffice, it will display results as you want :
SELECT books.book_title, authors.author_name, comments.comment FROM books
INNER JOIN books_authors
ON books.book_id=books_authors.book_id
INNER JOIN authors
ON books_authors.author_id=authors.author_id
LEFT JOIN comments_users ON comments_users.book_id = books.book_id
INNER JOIN comments ON comments_users.comment_id = comments.comment_id
INNER JOIN users ON comments_users.user_id = users.user_id
WHERE books.book_id=7

Can't you just JOIN all the tables from the two queries?
SELECT `books.book_id`, `books.book_title`, `comments_users.user_name`,
`comments_users.comment`, `comments_users.date`
FROM books
INNER JOIN books_authors ON books.book_id=books_authors.book_id
INNER JOIN authors ON books_authors.author_id=authors.author_id
INNER JOIN comments_users ON comments_users.book_id = books.book_id
INNER JOIN comments ON comments_users.comment_id = comments.comment_id
INNER JOIN users ON comments_users.user_id = users.user_id
WHERE books.book_id=7
Hope this is what you're trying to achieve.

Related

Error joining 3 tables php/sql

I'm new to this, so I know I'm missing something simple, but I can't figure it out. I'm trying to join 3 tables together and I've got it working with 2 joins, but when combined in the same query, there ends up being an error.
My 3 tables are:
TBL_Authors
Author_ID
Author_Name
TBL_Publishers
Publisher_ID
Publisher_Name
TBL_Books
Title
Author_ID
Publisher_ID
ISBN
Genre
Price
Cost
Rating
What I have that isn't working:
$query = 'SELECT * FROM TBL_PUBLISHERS
JOIN TBL_BOOKS ON TBL_PUBLISHERS.Publisher_ID = TBL_BOOKS.Publisher_ID
SELECT * FROM TBL_AUTHORS
JOIN TBL_BOOKS ON TBL_AUTHORS.Author_ID = TBL_BOOKS.Author_ID
ORDER BY TBL_BOOKS.Title ASC;';
This query assumes that each book was published.
SELECT
*
FROM
TBL_Books b
INNER JOIN TBL_Publishers p ON b.Publisher_ID = p.Publisher_ID
INNER JOIN TBL_Authors a ON b.Author_ID = a.Author_ID
ORDER BY
b.Title
Book will always have an Author, but not necessarily a Publisher, if it's not published. If you need to fetch all the books irrespective of whether published or not, you will have to change INNER join on TBL_Publishers to LEFT.
This Query Will show you the result of all details of book, publisher name, author name.
SELECT
t1.* , t2.Publisher_Name , t3.Author_Name
FROM
TBL_Books as t1
INNER JOIN TBL_Publishers as t2 ON t1.Publisher_ID = t2.Publisher_ID
INNER JOIN TBL_Authors as t3 ON t1.Author_ID = t2.Author_ID
ORDER BY
t1.Title
Check This and update if this query helps you.

Getting data from three tables using join mysql

I have three tables in my database
1.Users (id,username)
2.Stories (id,user_id,content) here user_id is forigen key refers Users table id
3.Comments (id,user_id,story_id,comment) here user_id is forigen key refers Users table id and story_id is forigen key refers Stories table id
I need to get list of stories from Stories table with total number of comments on that post and username of the storie author
Here is my query for that
SELECT stories.id,stories.content,COUNT(stories.id) as totalcomment
FROM stories
JOIN comments
ON stories.id=comments.story_id GROUP BY stories.id
I will get the total comment of the each post ,but can't fetch the username of the storie author (ie username from Users table)
SELECT s.id,s.content,COUNT(c.story_id) as totalcomment, u.username
FROM stories s LEFT JOIN comments c ON (s.id=c.story_id)
LEFT JOIN users u ON (s.user_id = u.id)
GROUP BY s.id
Try this:
SELECT stories.id,stories.content,COUNT(stories.id) as totalcomment, Users.username
FROM stories, Users, comments
WHERE stories.id=comments.story_id
AND stories.user_id = Users.id
GROUP BY stories.id
Or with join:
SELECT stories.id,stories.content,COUNT(stories.id) as totalcomment, Users.username
FROM stories
JOIN comments ON stories.id=comments.story_id
JOIN users ON stories.user_id = Users.id
GROUP BY stories.id
Hope this works.

Resolve data for 2 tables

Let's say, I have 3 tables. "users", "topics" and "replies.
The "topics" and "replies" table do have a column "user_id" which references to "id" on the "users" table. The "replies" table also have a column "topic_id" which references "id" on "topics". Also imagine that each topic has 1 reply.
Now, I want to fetch all topics and resolve the username for both tables, so the output should include a "topic_username" and a "reply_username".
I know how to select the username for the topic, but how can I do that for both the topics and replies table?
Thanks
Updated code:
SELECT
t.*,
t_users.username as topic_user,
last_reply.user_id as last_reply_user_id,
r_users.username as last_reply_username,
last_reply.replies_count,
ORDER BY IFNULL(last_reply.created_at,t.created_at DESC
FROM
topics as t
left join users as t_users on t_users.id = t.user_id
left join ( select r.*,count(r.id) as replies_count from (select * from replies order by id desc) as r group by r.topic_id ) as last_reply on last_reply.topic_id = t.id
left join users as r_users on r_users.id = last_reply.user_id
Try this
SELECT
t.*,
t_users.name as topic_user,
r_users.name as reply_user
FROM
topics as t
left join replies as r on r.topic_id=t.id
left join users as t_users on t_users.id = t.user_id
left join users as r_users on r_users.id = r.user_id
this would work well if there is 1:1 topic and reply, if you have 1 to many replies then you are better off group by topic id and get the usernames by group concat
Version 2 (to get reply count and last reply user)
SELECT
t.*,
t_users.name as topic_user,
last_reply.user_id as last_reply_user_id,
r_users.name as last_reply_username,
last_reply.replies_count,
IFNULL(last_reply.created_at,t.created_at) as last_update
FROM
topics as t
left join users as t_users on t_users.id = t.user_id
left join ( select r.*,count(r.id) as replies_count from (select * from replies order by id desc) as r group by r.topic_id ) as last_reply on last_reply.topic_id = t.id
left join users as r_users on r_users.id = last_reply.user_id
ORDER BY IFNULL(last_reply.created_at,t.created_at) DESC
You wanna do this in a single query? You can do it like this but I believe a separate query for fetching replies and topics might be better (fetch replies first, get the unique topic IDs and then fetch topics in a separate query)
SELECT r.id, r.topic_id, ru.username AS reply_username, tu.username AS topic_username
FROM replies AS r
INNER JOIN topics AS t ON (t.id = r.topic_id)
INNER JOIN users AS ru ON (r.user_id = u.id)
INNER JOIN users AS tu ON (t.user_id = u.id)

Slow mysql of a PHP Board

I have multiple tables of a php board.
I need an effective query, to select all categories, all topics, last post of topics, with the posted user. With my query it takes 5-8 seconds to run it.
I did an optimize with last_post_id field into topics table, but I need a better solution for it.
Structure
forum_categories ~ 15 lines
id|name|...
forum_topics ~ 150 lines
id|name|category_id|...
forum_posts ~ 1.000.000 lines
id|body|topic_id|user_id|...
users ~ 30.000 lines
id|username|...
category 1
- topic 1
- last post1 | user1
- topic 2
- last post2 | user2
...
category 2
- topic 3
- last post3 | user3
...
...
Last query (This was a help from my friend. But this also was so slow. )
SELECT c.NAME AS category,
t.NAME AS topic,
p.body AS post,
p.username AS username
FROM forum_categories AS c
JOIN forum_topics AS t
ON t.category_id = c.id
JOIN (SELECT *
FROM (SELECT p.body,
p.topic_id,
u.username
FROM forum_posts AS p
JOIN users AS u
ON u.id = p.user_id
ORDER BY p.id DESC) AS t
GROUP BY topic_id) AS p
ON t.id = p.topic_id
Exaplain query
Statistic of query
Headers are: sort, status, time | status, all time, pct. time, calls, time
I think "last post of topics" is key point of your query. That's why you used ORDER BY on most inner query but this makes two sub-queries.
Updated version
CREATE TEMPORARY TABLE last_post_per_topic_t ENGINE = MEMORY
SELECT topic_id, MAX(id) AS id -----+
FROM forum_posts --> find last post id per topic
GROUP BY topic_id; -----------------+
ALTER TABLE last_post_per_topic_t ADD INDEX (id, topic_id);
SELECT *
FROM forum_categories AS c INNER JOIN forum_topics t ON c.id = t.category_id
INNER JOIN forum_posts p ON p.topic_id = t.id
INNER JOIN last_post_per_topic_t ON last_post_per_topic_t.topic_id = t.id
AND last_post_per_topic_t.id = p.id;
INNER JOIN users u ON p.user_id = u.id;
first version
SELECT *
FROM forum_categories AS c INNER JOIN forum_topics t ON c.id = t.category_id
INNER JOIN forum_posts p ON p.topic_id = t.id
INNER JOIN (
SELECT topic_id, MAX(id) AS id -----+
FROM forum_posts --- find last post_id per topic
GROUP BY topic_id ---------------+
) last_post_per_topic_t ON last_post_per_topic_t.topic_id = t.id
AND last_post_per_topic_t.id = p.id;

PHP/MySQL foreign keys and displaying table

I have 3 tables that reference each other.. Leagues, Teams, and Players.
Leagues have leagueid, leaguename, city
Teams have teamid, teamname, numberofplayers, leagueid(fk)
Players have playerid, playername, position, teamid(fk)
How can I display a table such as Players to show all of the contents and using the teamid foreign key, display their team name instead of that column?
SELECT Players.*, teamname
FROM Players JOIN Teams USING (teamid)
You just need to join on the teamid column that matches between the two tables.
You need to join both tables use INNER JOIN.
SELECT a.*, b.*
FROM Players a
INNER JOIN Teams b
ON a.teamid = b.teamid
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
By using inner join
SELECT b.teamname,a.playername, a.position
FROM Players a
INNER JOIN Teams b
ON a.teamid = b.teamid
If you are specifying the league,
SELECT c.leaguename, c.city ,d.* FROM
(SELECT b.teamname,a.playername, a.position
FROM Players a
INNER JOIN Teams b
ON a.teamid = b.teamid) d
INNER JOIN Leagues c
ON d.leagueid= c.leagueid

Categories