2011-04-01 102 views
-3

我現在用的是2個以下MySQL表:mysql的連接查詢

樣品tbl_customers數據集

+----+--------+---------------+----------+------------+--------+------------+ 
| id | name | email   | phone | product_id | year | dnt  |   
+----+--------+---------------+----------+------------+--------+------------+ 
| 2 | tester | [email protected] | 12345678 | 75   | 2010 | 2011/01/01 | 
+----+--------+---------------+----------+------------+---------------------+ 

樣品tbl_products數據集

+----+----------------+---------------+--------------+ 
| id | name   | cost   | dnt   |   
+----+----------------+------------------------------+ 
| 75 | product1  | 500   | 2011/01/01 | 
+----+----------------+---------------+--------------+ 

想加入這兩個表。

謝謝!

回答

0

嘗試

SELECT c.id,c.email,c.phone,CONCAT(c.year,c.id) AS customer_ID,p.name 
FROM table_customer c 
JOIN table_product p 
ON p.id=c.product_id 
+0

從表名添加 – seoppc 2011-04-01 10:40:21

1
SELECT *, 
CONCAT(tbl_customers.year, tbl_customers.id) as customer_id, 
product_name as tbl_products.name, 
customer_id as tbl_customers.id, 
FROM tbl_customers 
JOIN tbl_products ON (tbl_customers.product_id = tbl_products.id) 
+0

你錯過了 「CUSTOMER_ID(CONCAT(YEAR和ID))」部分 – 2011-04-01 10:11:28

+0

加了吧,謝謝 – powtac 2011-04-01 10:12:13

+0

不客氣:) – 2011-04-01 10:12:57

0
select c.id, c.email, c.phone, CONCAT(c.year,' ',c.id), p.name 
from tbl_customers c inner join tbl_products p 
on c.product_id = p.id 
+0

感謝您的支持。 – seoppc 2011-04-01 10:33:49

+0

你甚至可以在查詢中添加表名。 – seoppc 2011-04-01 10:39:10

+0

@seoppc - 已添加。但你打算做什麼?這不夠嗎? – 2011-04-01 10:54:16