簡體   English   中英

從第一個表中選擇數據並將其與第二個表進行比較

[英]Select data from first table and compare it with second one

我這里有問題,我無法理解這東西是如何工作的。 所以這是交易:

$q= mysql_query("SELECT * FROM fruits_list, fruits_have WHERE fruits_list.fruit != fruit_have.fruit AND fruit_list.color != fruit_have.color");

表格:

fruits_list
- fruit --- color   -
-   1    -     2    -
-   2    -     3    -
-   1    -     3    -
-   1    -     4    -
---------------------

fruits_have
- fruit --- color -
-   1   ---   2   -
-   1   ---   3   -
-------------------

所以現在這個查詢將從“fruits_list”中刪除“fruit”中包含“1”和“color”中包含“2”、“3”的所有內容。

但我只想刪除兩列相等的車道。 在這個例子中,只應該從“fruits_list”中刪除 2 個車道,結果應該是這樣的:

fruits_dont_have
- fruit --- color -
-  2    -    3    -
-  1    -    4    -
-------------------

所以問題是,我應該在查詢中更改什么? 我希望我自己說得足夠清楚,讓你明白。

編輯:

Ryan E - 簡單地從結果中刪除。

bonCodigo - 兩者都是整數。

理想情況下,您將使用 SQL MINUS集合運算符,但 MySQL 尚不支持此操作。

您應該能夠通過左連接和 WHERE 來過濾掉匹配項來做到這一點:

SELECT fl.*
FROM fruits_list fl
LEFT JOIN fruits_have fh 
   ON fl.fruit = fh.fruit AND fl.color = fh.color
WHERE fh.fruit IS NULL

嘗試使用分隔符連接兩列,然后進行如下比較:

     SELECT * FROM fruits_list, fruits_have 
     WHERE CONCAT_WS('@',fruits_list.fruit, fruits_list.color) !=  
               CONCAT_WS('@',fruits_have.fruit, fruits_have.color)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM