select语句where条件(选择全部或部分)

时间:2013-04-01 09:49:30

标签: mysql sql database

我必须遵循以下表格:

图片:

imageID    userId    Translated    theImage
--------------------------------------------
    1         2         1           image1
    2         3         0           image2
    3         3         0           image3 
    4         3         0           image4
    5         3         1           image5

和translationChains:

imageID    sourceLang    targetLang
------------------------------------
   1           2            3
   2           4            1
   3           5            1
   4           4            2
   5           1            4          

现在我有两个选择: 我想要为任何语言的特定用户选择未翻译的所有图像 或者为该用户选择未以特定语言翻译的所有图像。

我对第一个选项有以下查询:

"Select images.imageid, images.theimage, images.translationRating 
From images where images.userID=? And images.translated =0"

并且我有第二个选项:

"Select images.imageid, images.theimage, images.translationRating 
From images, translationchains  
where images.userID=? And images.translated =0 and
translationchains.imageId = images.imageid
and translationchains.targetLang = ? " 

例如,如果我将第一个查询与用户3一起使用,我希望结果为:

2         3         0           image2
3         3         0           image3 
4         3         0           image4

对于用户3和targerLang = 1的第二个查询,我希望结果为:

    2         3         0           image2
    3         3         0           image3 

我想将这两个查询合并到一个查询中,该查询将适用于所有情况(根据我将发送的参数)

我该怎么办?

我试图发送第二个参数(translationchains.targetLang =?)字符串“”(空字符串),所以它会忽略这个条件,但它不起作用

3 个答案:

答案 0 :(得分:1)

你可以试试这个

第一次查询

      Select * From images where userID=3 And translated =0

DEMO HERE

输出:

  imageID   userId  Translated  theImage 
   2          3          0      image2
   3          3          0      image3
   4          3          0      image4

第二次查询

   Select i.imageID, userId, Translated, theImage 
   From images i
   inner join translationchains  t
   on t.imageId = i.imageid
   where i.userID=3 And i.translated =0 
   and t.targetLang = 1 

DEMO HERE

输出:

 imageID    userId  Translated  theImage 
    2         3         0       image2
    3         3         0       image3

THE COMBINE:

 Select i.imageID, userId, Translated, theImage 
 From images i
 inner join translationchains  t
 on t.imageId = i.imageid
 where i.userID=3 And i.translated =0 
 and t.targetLang in (select targetLang from translationChains  )
                                ^^^^^-------replace this by 1 or 2 or 3 or let it like that for all

    //-- if you want change targetLang just replace it by any number
    //--like that (select 1 from translationChains  )

DEMO HERE

OBS:当您选择语言然后放入

时,您也可以在php中执行此操作
   $sql .= "AND t.targetLang = the_id_selected_of_language ";

答案 1 :(得分:0)

尝试以下查询

SELECT images.imageid, images.theimage, images.translationRating 
FROM images
INNER JOIN translationchains ON translationchains.imageId = images.imageid
WHERE images.userID=? 
AND (
    (images.translated =1 AND translationchains.targetLang = ?) 
    OR (images.translated =0)
)

答案 2 :(得分:0)

尝试以下查询

SELECT a.imageid, a.theimage , a.translationRating
FROM images a , translationchains b
WHERE a.imageId = b.imageid
AND a.translated = 0
GROUP BY b.targetLang

以上查询将根据目标语言

对结果进行分组