我正在尝试为图片设置评级应用。我想显示当前登录用户尚未评级的图像。
这包括两个表,我已经成功地加入了表格但是额外的条件:
它显示结果,好像两个AND条件根本不存在。
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
AND ver_data_media.type = 'image'
AND ver_data_rating.a != $current_user->ID
非常感谢任何建议,谢谢弗兰克。
答案 0 :(得分:2)
您可以尝试以下方法吗?选择当前用户的记录,然后显示评级为空/空的图像......也许。如果你向我们展示了你的表格模式,那就更好了......这是一个更有效的答案。
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
。
例如您可以使用以下任一方法进行测试。
CAST(ver_data_rating.a AS INT) != CAST($current_user as INT)
CAST(ver_data_rating.a AS INT) <> = CAST($current_user as INT)
看起来,您的查询连接的方式,关系确实存在一些问题。请查看此示例,并告知我们您的结果。我列出了两种方法,可以从特定用户/用户的媒体表中获取未分级 media records
。变量用于存储用户ID。
<强> SQLFIDDLE DEMONSTRATION 强>
示例数据:
-- 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
来自user = 15下的评级表的评级记录ID,类型=图像
MID
1
使用IN子句:user = 15的未分级媒体记录,type = image`
ID VALUE REF TYPE
6 200 15 image
7 400 15 image
8 250 15 image
9 500 15 image
使用INNER JOIN:用户= 15,type = image
的未评级媒体记录ID VALUE REF TYPE
6 200 15 image
7 400 15 image
8 250 15 image
9 500 15 image
答案 1 :(得分:0)
左连接基本上是左外连接的缩写。您需要在JOIN条件之外将其他过滤器分隔到WHERE子句中。您的查询是说从ver_data_media获取所有记录。除此之外,给我ver_data_ratings符合我的条件或否则没有ver_data_ratings。很难翻译成英语:)我也认为,你所比较的列不正确或应该重命名。
试试这个......
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 OUTER JOIN ver_data_rating
ON ver_data_media.id= ver_data_rating.media_id
WHERE ver_data_media.type = 'image'
AND ver_data_rating.user_id <> $current_user->ID