使用php和mysql从两个不同的表中获取记录?
我有两张名为 1.表A使用php和mysql从两个不同的表中获取记录?,php,mysql,Php,Mysql,我有两张名为 1.表A 2.表B 我在下表中提供了详细信息: 表A: ====================== id | Name | ====================== 1 | Blue | ====================== 2 | Green | ====================== 3 | Red | ====================== 4 | Yellow| ====
2.表B 我在下表中提供了详细信息: 表A: ====================== id | Name | ====================== 1 | Blue | ====================== 2 | Green | ====================== 3 | Red | ====================== 4 | Yellow| ====================== 5 | Black | ====================== 6 | Orange| ====================== id |名称| ====================== 1 |蓝色| ====================== 2 |绿色| ====================== 3 |红色| ====================== 4 |黄色| ====================== 5 |黑色| ====================== 6 |橙色| 表B: ================================ id | table_id | value | ================================ 1 | 1 | x | ================================ 2 | 2 | z | ================================ 3 | 3 | W | ================================ id |表| id |值| ================================ 1 | 1 | x| ================================ 2 | 2 | z| ================================ 3 | 3 | W| 使用这两个表,我想显示来自以下两个表的值: SL | Name | ------------------------ 1 | Blue | ------------------------ 2 | Green | ------------------------ 3 | Red | ------------------------ 4 | Yellow | ------------------------ 5 | Black | ------------------------ 6 | Orange |
$query = mysql_query("Select *from table A LEFT JOIN table B ON a.id=b.table_id");
while ($newRow = mysql_fetch_array($query))
{
if $newRow['table_id']!=NULL
{
//You should white your bold code
}
else
{
//others
}
}
SL |名称|
------------------------
1 |蓝色|
------------------------
2 |绿色|
------------------------
3 |红色|
------------------------
4 |黄色|
------------------------
5 |黑色|
------------------------
6 |橙色|
在视图上方,粗体(蓝色、绿色、黄色)表示表B包含值表A
提前感谢。您可以使用LEFT JOIN:
select *
from A left join B on A.id=B.id
然后,在PHP中,可以用粗体打印
$row['table_id']!==null;
您可以编写如下所示的查询: SL | Name | ------------------------ 1 | Blue | ------------------------ 2 | Green | ------------------------ 3 | Red | ------------------------ 4 | Yellow | ------------------------ 5 | Black | ------------------------ 6 | Orange |
$query = mysql_query("Select *from table A LEFT JOIN table B ON a.id=b.table_id");
while ($newRow = mysql_fetch_array($query))
{
if $newRow['table_id']!=NULL
{
//You should white your bold code
}
else
{
//others
}
}
获取错误的数据,请参阅我上面给出的表。表A中的表id与表B中的表id相关。可能是您在查询中的条件应该是A.id=B.table\u id@hammurabi,无论如何,感谢您的回复。