0

两张表,brand_gifts bg,giftcode gc:

表 1:brand_gifts

id brand_id name

表 2:礼品码

id gift_id code usesleft

我用来检索数据的值是brand_gifts 表中的brand_id。我需要的是表一brand_id = 1上的元素列表,这两个表通过table1相关:id table2:gift_id(不需要table1上的所有元素都与table2有关系,换句话说,不是所有的id表一在表2上有一个gift_id),所以使用该关系我想查询所有brand_id = 1,其中也使用left> 0。

我试过这个:

SELECT gc.gift_id
FROM brand_gifts bg, giftcode gc
WHERE (
bg.brand_id =1
AND gc.gift_id = gc.id
AND gc.usesleft >0
)
LIMIT 0 , 30

但结果是错误的。

4

3 回答 3

1

试试这个

SELECT gc.gift_id
FROM brand_gifts bg, giftcode gc
WHERE (
bg.brand_id =1
AND gc.gift_id = bg.id
AND gc.usesleft >0
)
LIMIT 0 , 30
于 2013-06-24T08:20:19.733 回答
1
   SELECT gc.gift_id
    FROM brand_gifts bg, giftcode gc
   WHERE (
    bg.brand_id =1
    AND gc.gift_id = gc.id    <==
   AND gc.usesleft >0
   )
    LIMIT 0 , 30

您的错误看起来在标记的行中,条件应该是“gc.gift_id = bg.id”

   SELECT gc.gift_id
    FROM brand_gifts bg, giftcode gc
   WHERE (
    bg.brand_id =1
    AND gc.gift_id = bg.id    <==
   AND gc.usesleft >0
  )
    LIMIT 0 , 30
于 2013-06-24T08:30:13.920 回答
0

尝试

SELECT gc.gift_id
FROM giftcode gc
LEFT JOIN brand_gifts bg
ON gc.gift_id = bg.id
WHERE gc.usesleft > 0
于 2013-06-24T08:25:15.543 回答