你可以试试下面的吗?选择当前用户的记录,然后显示评级为空/空的图像......也许。如果您向我们展示了您的表格架构,那么您会更好......以获得更有效的答案。
SELECT ver_data_media.id, ver_data_media.value,
ver_data_media.ref, ver_data_media.type, ver_data_rating.a
FROM ver_data_media
LEFT JOIN ver_data_rating
ON ver_data_media.ref = ver_data_rating.a
WHERE ver_data_media.type = 'image'
AND ver_data_rating.a is null
AND id = $current_user -- shouldn't this be like userid = $current_user,
-- where is this ID coming from? what is its relation to this table?
否则,最可能的问题是您正在验证的数据不是同一类型,ver_data_rating.a != $current_user->ID
在这一行。我想最好的建议是将一种数据类型同时转换为ver_Data_rating.a
和$current_user
。
例如,您可以使用以下任一方法进行测试。
更新:使用推荐的表架构和示例查询
看起来,您的查询连接方式确实存在一些问题。请看一下这个样本,让我们知道你的结果。我列出了两种从媒体表中获取特定用户/用户在线未评级的方法。 media records
变量用于存储用户 ID。
SQLFIDDLE 演示
样本数据:
-- media table
ID VALUE REF TYPE
1 300 15 image
2 100 25 art
3 500 35 cartoon
4 200 35 image
5 100 25 image
6 200 15 image
7 400 15 image
8 250 15 image
9 500 15 image
10 180 15 art
-- rating table
RID MID UID SOMETHING
100 1 15 xyz
101 2 25 abc
102 5 25 efg
103 3 35 abc
逐步查询:
set @user_id:=15;
-- find all media records for @user, type image
select * from media
where ref = @user_id
and type = 'image'
;
-- find all rated reords for @user
select mid from rating
where uid = @user_id
;
-- media records that needs to be rated by @user
-- using IN clause
select * from media
where id not in
(select mid from rating
where uid = @user_id)
and type = 'image'
and ref = @user_id
;
-- media records that needs to be rated by @user
-- using INNER JOIN
select m.*
from media m
inner join (
select mid from rating
where uid = @user_id) as x
on m.id <> x.mid
and m.type = 'image'
and m.ref = @user_id
;
上述每一步查询的结果:
ID VALUE REF TYPE
1 300 15 image
6 200 15 image
7 400 15 image
8 250 15 image
9 500 15 image
用户 = 15 下评级表中的评级记录 ID,类型 = 图像
MID
1
使用 IN 子句:用户 = 15,类型 = 图像的未分级媒体记录
ID VALUE REF TYPE
6 200 15 image
7 400 15 image
8 250 15 image
9 500 15 image
使用 INNER JOIN :用户 = 15 的未分级媒体记录,类型 = 图像
ID VALUE REF TYPE
6 200 15 image
7 400 15 image
8 250 15 image
9 500 15 image