如何从3个表中检索mysql数据库中的值?

时间:2013-01-09 18:18:29

标签: mysql database select

用户表:

userID | name
1      | John
2      | Mary
3      | Jack

项目表(用户将选择并存储到mysql

itemID | name   | item 
1      | Mary   | pants, jeans
1      | John   | jacket, shirt
1      | Jack   | jacket, shirt

描述表(根据用户选择项目检索描述)

descID | item          | descOne | descTwo
1      | jacket, shirt | Nice!   | Great   
1      | pants, jeans  | good    | Nice    
1      | dress, jeans  | Nice!   | Great   

如果我想检索Mary的值,我的select sql语句应该是什么?

目前我有这个 - >

SELECT 
    d.item, 
    d.descOne, 
    d.descTwo, 
    i.name , d.item
FROM description d 
INNER JOIN items i 
WHERE d.item = i.item ON;

1 个答案:

答案 0 :(得分:2)

SELECT 
    u.name,
    d.descOne, 
    d.descTwo, 
    d.item
FROM users as u
LEFT JOIN items as i ON i.name = u.name
LEFT JOIN description as d ON d.item = i.item
WHERE u.name = 'Mary'