[英]Joining 3 Tables, mysql php
我如何加入所有三個表? 我不知道,因為我需要把它們全部都放在一張桌子上
顧客
+--------+------------+---------------+---------+---------+
| serial | name | email | address | phone |
+--------+------------+---------------+---------+---------+
| 1 | first_name | email@web.com | address | 7777777 |
+--------+------------+---------------+---------+---------+
命令
+--------+------------+------------+
| serial | date | customerid |
+--------+------------+------------+
| 1 | 2014-03-04 | 1 |
+--------+------------+------------+
訂單詳情
+---------+-----------+----------+-------+
| orderid | productid | quantity | price |
+---------+-----------+----------+-------+
| 1 | 1 | 30 | 400 |
| 1 | 2 | 10 | 500 |
+---------+-----------+----------+-------+
表orders
上的customerid
是表上customers
的序列,表上order_detail
上的orderid
是orders
上的序列
如果我使用另一個桌子怎么辦? 對於productid,其中另一個表中的productid = product_id?
幫助將不勝感激,我真的很抱歉桌子,我不知道如何在這里做桌子,但他們是有序的。
您只需要使用Join語句。 所以你可以做以下
Select * from Customers
join orders on Customers.serial = orders.customerid
join order_detail on orders.serial = order_detail.orderid
您還可以創建另一個名為Product
表,並按照我向您展示的相同方式進行聯接。
如果要從不同的表中選擇列,則必須使用tablename.columnname
。 因此,例如,如果您想從order_detail表中選擇數量和價格,請執行以下操作
Select * from Customers,order_detail.quantity,order_detail.price
join orders on Customers.serial = orders.customerid
join order_detail on orders.serial = order_detail.orderid
希望你明白我的意思。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.