mysql多表SELECT条件

时间:2013-08-05 22:02:47

标签: mysql

我一直在努力寻找以下情况的解决方案。 我有4张桌子

Table 1
product_id|product_name

Table 2
Review_id|listing_name|rating|content|product_id|user_id

Table3
user_id|user_name|

Table 4
user_id|rank|

product_id在表1和表2中具有相同的值 user_id在表2,3,4中具有相同的值

我需要帮助将以下语句放入一个数组

SELECT review_id, rating, content, user_name, rank FROM T2, T3, T4 WHERE              product_id=$pid and WHERE T3.user_id = T2.user_id 

我的数组应如下所示:

$arr[review_id] = $row[review_id]
$arr[rating] = $row[rating]
$arr[content] = $row[content]
$arr[user_name] = $row[user_name]
$arr[rank] = $row[rank]

所有帮助都会受到极大关注

1 个答案:

答案 0 :(得分:1)

SELECT T2.review_id, T2.rating, T2.content, T3.user_name, T4.tank
    FROM T2
    INNER JOIN T3
        ON T2.user_id = T3.user_id
    INNER JOIN T4
        ON T2.user_id = T4.user_id
    WHERE T2.product_id = $pid