首页 文章

使用php和mysql从两个不同的表中获取记录?

提问于
浏览
1

我有两个名为的表

1.表A
2.表B.

我在下面的表格中给出了详细信息:

表A:

======================
   id  |   Name  |
======================
   1   |   Blue  |
======================
   2   |   Green |
======================
   3   |   Red   |
======================
   4   |   Yellow|
======================
   5   |   Black |
======================
   6   |   Orange|

表B:

================================
  id |  table_id  |  value   |
================================
  1  |   1        |   x      |
================================
  2  |   2        |   z      |
================================
  3  |   3        |   W      |

使用这两个表,我想显示来自两个表的值,如下所示:

SL      |    Name   |
------------------------
 1       |    Blue   |
------------------------
 2       |    Green  |
------------------------
 3       |    Red    |
------------------------
 4       |    Yellow |
------------------------
 5       |    Black  |
------------------------
 6       |    Orange |

在视图上方,粗体(蓝色,绿色,黄色)表示表B包含值表A.

提前致谢 .

2 回答

  • 0

    您可以编写如下的查询:

    $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
       }
    }
    
  • 0

    你可以使用LEFT JOIN:

    select *
    from A left join B on A.id=B.id
    

    然后,在PHP中,您以粗体打印任何行

    $row['table_id']!==null;
    

相关问题