多个表上的内连接不返回行

时间:2016-09-15 15:17:59

标签: mysql sql

此查询有什么问题吗?我一直在工作,直到我截断我的数据库,现在我只有一个记录,当我在phpmyadmin中运行此查询时,它返回0行。

我错过了什么?

SELECT 
Users.id,
Users.firstname,
Users.lastname,
Users.email,
Users.age,
Users.phone,
Universities.Faculty AS university,
Internship.internship_type AS Internship,
City.city AS city,
Interest.interest AS interest,
Users.filename,
Users.reg_date 
        FROM Users 
        INNER JOIN City ON Users.city = City.key 
        INNER JOIN Universities ON Users.university = Universities.id 
        INNER JOIN Interes ON Users.interest = Interest.key 
        INNER JOIN Internship ON Users.internship_type = Internship.internship_type
            ORDER BY `Users`.`id` ASC;

1 个答案:

答案 0 :(得分:2)

所有表中可能都没有数据,所以你可以使用左连接检查它,并确保至少用户表必须有数据:然后逐个更改左连接到内部,你不会获得数据会有问题,但如果其他表的数据是可选的,那么最好使用左连接:

SELECT 
    Users.id,
    Users.firstname,
    Users.lastname,
    Users.email,
    Users.age,
    Users.phone,
    Universities.Faculty AS university,
    Internship.internship_type AS Internship,
    City.city AS city,
    Interest.interest AS interest,
    Users.filename,
    Users.reg_date 
FROM Users 
LEFT JOIN City ON Users.city = City.key 
LEFT JOIN Universities ON Users.university = Universities.id 
LEFT JOIN Interes ON Users.interest = Interest.key 
LEFT JOIN Internship ON Users.internship_type = Internship.internship_type
ORDER BY `Users`.`id` ASC;