-1

如何使这种语法有效:

SELECT * 
FROM
  uploads,
  audienceuploadassociation 
WHERE 
  uploads.member_id = '1'
  AND uploads.member_school_id='1' 
  AND subject = 'Maths' 
  AND uploads.upload_id = audienceuploadassociation.upload_id 
  AND topic = 'Integers' 
  AND year IN(7, 8, 9) 
  AND audienceuploadassociation.audiencename LIKE (Parents, Teachers, Community)
4

2 回答 2

3

你的LIKE条款有问题。我怀疑您打算改用一个IN()子句

AND audienceuploadassociation.audiencename IN ('Parents', 'Teachers', 'Community')

如果您尝试将部分字符串与 匹配LIKE,则需要将OR它们放在一个()组中,如下所示:

AND (
  audienceuploadassociation.audiencename LIKE 'Parents%'
  OR audienceuploadassociation.audiencename LIKE 'Teachers%'
  OR audienceuploadassociation.audiencename LIKE 'Community%'
)

另请注意,您使用的隐式连接语法(逗号分隔的表)已被弃用,取而代之的是显式的JOIN.

SELECT
  uploads.*,
  audienceuploadassociation.*
FROM
  /* Explicit JOIN  with ON clause */
  uploads
  JOIN audienceuploadassociation ON uploads.upload_id = audienceuploadassociation.upload_id 
WHERE 
  uploads.member_id = '1'
  AND uploads.member_school_id='1' 
  AND subject = 'Maths' 
  AND topic = 'Integers' 
  AND year IN(7, 8, 9) 
  AND audienceuploadassociation.audiencename IN ('Parents', 'Teachers', 'Community')

最后,我建议明确命名您想要的列而不是这样做SELECT *,因此您可以按确定的顺序获取它们,并防止稍后在此查询中添加您不需要的其他列:

SELECT 
  uploads.upload_id,
  uploads.other_column,
  uploads.topic,
  audienceuploadassociation.audiencename,
  ...
  ...
于 2012-05-25T16:07:12.143 回答
0

这行得通吗?

SELECT * 
FROM
  uploads,
  audienceuploadassociation 
WHERE 
  uploads.member_id = '1'
  AND uploads.member_school_id='1' 
  AND subject = 'Maths' 
  AND uploads.upload_id = audienceuploadassociation.upload_id 
  AND topic = 'Integers' 
  AND year IN(7, 8, 9) 
  AND audienceuploadassociation.audiencename IN ('Parents', 'Teachers', 'Community');
于 2012-05-25T16:07:39.100 回答