从一个表中获取列引用的唯一行在另一个表中没有

时间:2014-08-28 11:04:33

标签: php mysql sql

我有两张桌子名orders
第二个名为printing
这样的orders表结构

id job_code job_name qty
1 597 xyz 1000
2 598 lmn 2500
3 599 oqr 20000
4 600 odc 15000

像这样的

printing表结构

id job_code dispatch qty
1 598 yes 1800
2 600 yes 1456

我想从job.code

中选择orders来自printing的所有SELECT DISTINCT orders.job_code, orders.job_name, orders.qty FROM orders INNER JOIN printing ON orders.job_code <> printing.job_code ORDER BY orders.job_code DESC LIMIT 10;

我尝试了这个查询。

SELECT DISTINCT orders.job_code, orders.job_name, orders.qty FROM orders  
INNER JOIN printing
ON orders.job_code NOT IN (printing.job_code) ORDER BY orders.job_code DESC
LIMIT 10;

OR

orders

但它会返回printing和{{1}}表格上的所有作业

3 个答案:

答案 0 :(得分:1)

select job_code from orders 
where job_cobe not in (select job_code from printing)

答案 1 :(得分:1)

您可以使用此查询。

SELECT
    DISTINCT orders.job_code, orders.job_name, orders.qty 
FROM
    orders 
WHERE
    orders.job_code
    NOT IN
        (SELECT printing.job_code FROM printing)
ORDER BY
    orders.job_code DESC
LIMIT 10

答案 2 :(得分:0)

使用像这样的查询

SELECT job_code FROM `orders` left join printingorders on orders.job_code not in ( select job_code from printingorders)

这会给你结果。我试着用我的Phpmyadmin。