三个表之间的一个mysql查询

时间:2014-04-21 18:11:23

标签: php mysql sql

我有三张桌子

1.Users
-User_id
2.Books
-Book_id
-Book_name
-User_id
3.Likes
-Other_User_id
-Book_id

我只知道' Users.user_id'我想知道Books表中的likes表中每本书的书名和Like Counts。

注意:用户写书和其他用户一样。因此产生的JSON数组会像..我输入mysql只有user_id = 10并且它打印....

{User id: 10 , Username: cemsitcan, Book Name: Sample, Book liked:120 times.}
{User id: 10 , Username: cemsitcan, Book Name: Sample1, Book liked:10 times.}
{User id: 10 , Username: cemsitcan, Book Name: Sample2, Book liked:123 times.}

2 个答案:

答案 0 :(得分:0)

您不需要User表格。如果您使用此查询:

SELECT *, (
        SELECT COUNT(`Other_User_id`)
           FROM `Likes`
           WHERE `Books`.`Book_id`=`Likes`.`Book_id`
    ) AS `Likes_Count`
    FROM `books`
    WHERE `User_id`=10;

小提琴:

CREATE TABLE Books
    (`Book_id` int, `Book_name` varchar(30), `User_id` int)
;

INSERT INTO Books
    (`Book_id`, `Book_name`, `User_id`)
VALUES
    (1, 'Hello World by 5', 5),
    (2, 'Hello World by 10', 10),
    (3, 'Microsoft by 7', 7),
    (4, 'Its a Small World by 10', 10),
    (5, 'StackOverflow is Great by 10', 10)
;

CREATE TABLE Users
    (`User_id` int, `User_name` varchar(15))
;

INSERT INTO Users
    (`User_id`, `User_name`)
VALUES
    (1, 'Praveen'),
    (5, 'User'),
    (10, 'Praveen Kumar')
;

CREATE TABLE Likes
    (`User_id` int, `Book_id` int)
;

INSERT INTO Likes
    (`User_id`, `Book_id`)
VALUES
    (1, 5),
    (1, 1),
    (1, 10),
    (2, 10),
    (3, 5),
    (3, 10)
;

查询:

SELECT *, (
        SELECT COUNT(`User_id`)
           FROM `Likes`
           WHERE `Books`.`Book_id`=`Likes`.`Book_id`
    ) AS `Likes_Count`
    FROM `books`
    WHERE `User_id`=10;

输出:

+---------+------------------------------+---------+-------------+
| BOOK_ID |                    BOOK_NAME | USER_ID | LIKES_COUNT |
+---------+------------------------------+---------+-------------+
|       2 |            Hello World by 10 |      10 |           0 |
|       4 |      Its a Small World by 10 |      10 |           0 |
|       5 | StackOverflow is Great by 10 |      10 |           2 |
+---------+------------------------------+---------+-------------+

如果您希望作者显示结果,您需要:

SELECT COUNT(`User_id`)
           FROM `Likes`
           WHERE `Books`.`Book_id`=`Likes`.`Book_id`
    ) AS `Likes_Count`, (
        SELECT `User_name`
           FROM `Users`
           WHERE `Users`.`User_id`=`Books`.`User_id`
    ) AS `Author`
    FROM `Books`
    WHERE `User_id`=10;

输出结果为:

+---------+------------------------------+---------+-------------+---------------+
| BOOK_ID |                    BOOK_NAME | USER_ID | LIKES_COUNT |        AUTHOR |
+---------+------------------------------+---------+-------------+---------------+
|       2 |            Hello World by 10 |      10 |           0 | Praveen Kumar |
|       4 |      Its a Small World by 10 |      10 |           0 | Praveen Kumar |
|       5 | StackOverflow is Great by 10 |      10 |           2 | Praveen Kumar |
+---------+------------------------------+---------+-------------+---------------+

小提琴:http://www.sqlfiddle.com/#!2/cb1a4b/3

答案 1 :(得分:0)

select b.user_id, b.book_name, count(l.book_id) from Books b
inner join likes l on l.book_id = b.book_id
Group by b.user_id, b.book_name
Where b.user_id = ?

此外,你的json输出应该只提供命名良好的值,在那里坚持格式信息就像'时代'更不用说名字中的空格是一个非常糟糕的主意。让消费数据的任何东西选择如何呈现它。