如何在MySQL中组合两个表

时间:2016-07-05 04:36:12

标签: mysql merge

我有两个表(tbl_CUST,tbl_Total)

tbl_CUST

Name   Sex  ProductName   Price
Jonh   M    iPhone 6s     $ 1200
Smith  M    iPhone 6      $ 900
Mary   F    Samsung S7    $ 800

tbl_total (只有一条记录)

Total   Date             
$2,900   20-05-2016

This the result that I did. But I don't need like that:
            Name   Sex  ProductName   Price    Total    Date
            Jonh   M    iPhone 6s     $ 1,200   $2,900  20-05-2016
            Smith  M    iPhone 6      $ 900
            Mary   F    Samsung S7    $ 800

 Now I need to combine table and show the result like that: 
            Name   Sex  ProductName   Price    Total    Date
            Jonh   M    iPhone 6s     $ 1,200   $2,900  20-05-2016
            Smith  M    iPhone 6      $ 900
            Mary   F    Samsung S7    $ 800

 So, What's can I do? Please help me! Thanks so much!

1 个答案:

答案 0 :(得分:0)

$ sql = mysql_query(tbl_CUST上的{select * from tbl_Total内部加入tbl_TotalName = tbl_CUSTName“, $ conn);在

在查询中使用内部联接进行拖动表合并。这里的“NAME”是两个表的条​​件,其中查询只返回同名记录,只有其他记录是hide。