我正在做的是:获取数据以向用户显示产品,以便他/她可以对产品进行评级。使用以下查询可以正常工作。
$query = " SELECT table_products.product_name,
table_products.product_expiry,
table_users.name,
table_users.email
FROM table_products
INNER JOIN table_users ON table_users.user_id = table_products.user_id
ORDER BY table_users.id DESC";
现在我想问的是:如何仅显示用户尚未评级的产品。用户对产品评分后,该用户的查询不应再次提取该产品。
我有3张桌子
table_users (user_id, name, email)
table_products (product id_, user_id, product_name, product_expiry)
table_rating (rating_id, user_id, product_id, rating)
我想从2个表中获取数据
table_users, table_products
要提取的数据
table_users.name,
table_users.email,
table_products.product_name,
table_products.product_expiry
如何为特定user_id
设置此任务的查询?
感谢您的时间和帮助:)
答案 0 :(得分:0)
您可以加入评级表并计算评分数量,然后在where子句中查看,如下所示(未经测试):
$query = "SELECT
table_products.product_name,
table_products.product_expiry,
table_users.name,
table_users.email,
count(table_rating.id) as ratings
FROM table_products
INNER JOIN table_users ON table_users.user_id = table_products.user_id
left join table_rating ON table_rating.product_id = table_products.id
where ratings = 0
ORDER BY table_users.id DESC";
答案 1 :(得分:0)
答案 2 :(得分:0)
使用此查询
SELECT table_products.product_name,
table_products.product_expiry,
table_users.name,
table_users.email
FROM table_products
INNER JOIN table_users ON table_users.user_id = table_products.user_id
LEFT JOIN table_rating on table_products.product_id=table_rating.product_id
WHERE table_rating.product_id IS NULL
ORDER BY table_users.id DESC
答案 3 :(得分:0)
SELECT
table_products.product_name,
table_products.product_expiry,
table_users.name,
table_users.email
FROM table_products
INNER JOIN table_users ON table_users.user_id = table_products.user_id
LEFT JOIN table_rating ON table_products.user_id = table_rating.user_id
AND table_products.product_id = table_rating.product_id
WHERE table_rating.product_id IS NULL
ORDER BY table_users.user_id DESC