MYSQL在条件不同的表中选择

时间:2013-08-28 09:11:16

标签: mysql sql select

更新

我知道变量$ 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
result = result5

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

sqlfiddle

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

enter image description here

sqlfiddle

答案 2 :(得分:0)

简单的连接将处理此问题。

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

你需要与表格有共同之处...... 例 表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');