php - SQL 2表Q?

扫码查看

This question already has answers here:
MySQL: Quick breakdown of the types of joins
                                
                                    (4个答案)
                                
                        
                                3年前关闭。
            
                    
我有2张桌子
工作
ID,说明,名称
测试
id,jobid
我创建了查询:

SELECT jobs.id,jobs.name,jobs.country,jobs.description,test.id,test.jobid
FROM jobs,test
WHERE jobs.userid='10'
AND
GROUP BY jobs.id


它回显了我所有的工作,但测试ID在所有1个中
如果我将其设置为where jobs.id=test.jobid,则结果为已知。
如何打印test.jobid等于job.id的所有作业以及没有test.jobid的作业

最佳答案

听起来您正在寻找的是左连接命令。

SELECT jobs.id AS jobs_id,jobs.name,jobs.country,jobs.description,test.id AS test_id,test.jobid AS job_id_from_test
    FROM jobs
    LEFT JOIN test ON test.jobid = jobs.id
    WHERE jobs.userid='10'


-可能有帮助。上面的代码添加了left join命令,并删除了不必要的“ AND”和“ GROUP BY”语句。与正常的JOIN不同,LEFT JOIN将返回作业表的所有适用值,即使测试表中没有相应的行也是如此。

关于php - SQL 2表Q? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39666696/

10-10 14:05
查看更多