簡體   English   中英

比較兩個表以在MySQL中查找不同的值

[英]Compare two tables to find different values in mySQL

我有兩個這樣的表:

表格1

|id| comments | class    |
|1 | bla bla  | positive |
|2 | bla bla  | negative |

表2

|id| comments | class    |
|1 | bla bla  | positive |
|2 | bla bla  | positive |

我要比較的class ,並將其存儲到變量,名為tp如果classtable 1table 2是積極的。
如果table 1class為負,而table 2為正,則它將存儲在fn
我已經嘗試過,但由於我是新手而無法做到。 我希望有人可以幫助我。 謝謝。


之后,我可以在php中使用tpfn進行一些計算。

如注釋中所述,您應該提供兩種情況的示例輸出,因為尚不清楚要在變量中使用哪一列。 使用如下所示的內容。

這是假設

  1. 您要在變量中輸入id

  2. 您想基於id列加入。

在變量中使用這些查詢的輸出。

注意:我不知道PHP,但是如果查詢返回的行多於1行,則可能必須使用數組或類似的東西才能在循環中分別獲取每一行。

select t1.id 
from table1 t1 
inner join table2 t2
on t1.id=t2.id
and t1.class='positive'
and t2.class='positive';

select t1.id 
from table1 t1 
inner join table2 t2
on t1.id=t2.id
and t1.class='negative'
and t2.class='positive';

暫無
暫無

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

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