0

我正在尝试为图像设置评分应用程序。我想显示当前登录用户尚未评分的图像。

这涵盖了两个表,我已经成功加入了这些表,但是额外的条件:

  1. 必须是图像
  2. 排除当前用户已评级的任何图像都不起作用。

它显示的结果好像 2 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

任何建议将不胜感激,谢谢弗兰克。

4

2 回答 2

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 演示

样本数据:

-- 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
于 2012-12-15T16:41:14.130 回答
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
于 2012-12-15T16:45:56.860 回答