簡體   English   中英

MYSQL在不同條件下選擇表

[英]MYSQL select in different table with condition

更新

我知道變量$ id是table_1中數據的ID。 我在table_1和table_2中有兩個相同的列(內容相同)。 我想選擇並顯示table_2中的列(結果)。

表格1

|     ID        |      color      | 
-----------------------------------
       1        |      data1      |     
       2        |      data2      |   
       3        |      data3      |     
       4        |      data4      |   
       5        |      data5      |    

表2

|     ID        |      flower     |      result      | 
------------------------------------------------------
       11       |      data1      |     result1      |    
       12       |      data2      |     result2      |     
       13       |      data3      |     result3      |      
       14       |      data4      |     result4      |       
       15       |      data5      |     result5      |    


ID = 5
結果= result5

Select t2.*, t1.color from t2 inner join t1 on t1.color = t2.data and t1.id = '$id'

sqlfiddle

select t1.id, t2.result
from table1 t1, table2 t2
where
t1.id = <your-id>
and
t1.color = t2.flower;

在此處輸入圖片說明

sqlfiddle

一個簡單的連接將處理此問題。

select t2.result
from table1 t1
join table2 t2
on t1.color = t2.flower
where t1.id = 5

SELECT result FROM t2 JOIN t1 ON (t1.color = t2.data AND t1.id = $id);

您將需要與表格有一些共同點……示例表1:將具有ID和顏色表2:將具有ID,ID_table1,花,結果

然后你可以這樣做:

$select1=mysql_query("select * from table1");
$id_table1=mysql_result($select1,0,'enter the id here');
$select2=mysql_query("select * from table2 where ID_table1='".$id_table1."'");
$result=mysql_result($select2,0,'result');

暫無
暫無

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

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