![](/img/trans.png)
[英]Copy a record with a unique reference in mysql from one table to another table
[英]Fetching unique rows from one table that columns reference has not in another table
我有兩個表,第一個名字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
我想從不在printing
表中的orders
選擇所有job.code
我嘗試使用此查詢。
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;
但是它將返回orders
和printing
表上保留的所有作業
select job_code from orders
where job_cobe not in (select job_code from printing)
您可以使用此查詢。
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
使用這樣的查詢
SELECT job_code FROM `orders` left join printingorders on orders.job_code not in ( select job_code from printingorders)
這將為您提供結果。 我在Phpmyadmin中嘗試過。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.