SQL查询 - 从一个表中选择所有匹配记录的表

时间:2017-01-04 15:52:30

标签: mysql sql

我知道如何从一个表中选择值,以及它们是否在另一个表中具有匹配值

echo <ESC>[93m Logging in

现在我遇到的问题是,如果第二个表中有多个记录,它会提供mutlitple记录

我有两张桌子

  • me_cards - 有
  • 中的卡片列表
  • me_member_cards - 拥有该成员所拥有的列表。

如果卡在会员卡中多次出现,则查询返回所有行,我希望它返回所有卡,如果用户有该卡。 当前,用户拥有该卡的ID,然后来自me_member_cards的详细信息显示,如果他们没有显示为空

SELECT *
FROM me_cards AS c LEFT JOIN
     me_member_cards AS m
     ON m.cardID = c.id AND m.userID = 2 AND m.status = 'Collecting' LEFT JOIN
     me_decks AS d
     ON d.deckName = c.deckName 
WHERE c.deckName = 'Couples'
ORDER BY cardNumber

我得到的是

id    deckName   cardNumber   id     userID   cardID   status       deckName   deckDescription            deckCategory   deckSubCat   deckType   deckFeatures   deckWorth   deckCardAmount   deckURLName   deckReleased   deckOrder   deckCard
202   Couples    01           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
203   Couples    02           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
204   Couples    03           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
205   Couples    04           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
206   Couples    05           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
207   Couples    06           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
208   Couples    07           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
209   Couples    08           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
210   Couples    09           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
211   Couples    10           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
212   Couples    11           29     2        212      Collecting   Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
213   Couples    12           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
214   Couples    13           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
215   Couples    14           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
216   Couples    15           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
217   Couples    16           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
218   Couples    17           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
219   Couples    18           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
220   Couples    19           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
221   Couples    20           81     2        221      Collecting   Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
221   Couples    20           83     2        221      Collecting   Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
221   Couples    20           101    2        221      Collecting   Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
221   Couples    20           80     2        221      Collecting   Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14

添加我想要的内容

id    deckName   cardNumber   id     userID   cardID   status       deckName   deckDescription            deckCategory   deckSubCat   deckType   deckFeatures   deckWorth   deckCardAmount   deckURLName   deckReleased   deckOrder   deckCard
202   Couples    01           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
203   Couples    02           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
204   Couples    03           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
205   Couples    04           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
206   Couples    05           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
207   Couples    06           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
208   Couples    07           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
209   Couples    08           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
210   Couples    09           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
211   Couples    10           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
212   Couples    11           29     2        212      Collecting   Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
213   Couples    12           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
214   Couples    13           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
215   Couples    14           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
216   Couples    15           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
217   Couples    16           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
218   Couples    17           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
219   Couples    18           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
220   Couples    19           NULL   NULL     NULL     NULL         Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      
221   Couples    20           81     2        221      Collecting   Couples    Senshi & Shitennout Pair   Puzzle         Manga        Puzzle                    1           20               couples       1              999         14      

我最终想要做的是获取套牌中的所有卡片,如果用户有卡片然后显示卡片,如果他们不显示占位符

2 个答案:

答案 0 :(得分:1)

Sooo ......这样的事情?

从卡中有成员的卡中返回所有卡,但只返回一次。包括来自Decks的列

select c.*, d.*
from me_cards c
inner join me_decks d
  on d.deckName = c.deckName 
where c.id in (select m.cardid 
               from me_member_cards m 
               where m.status = 'collecting')
and c.deckname = 'Couples'

编辑:

好的,所以......

select *
from me_cards c
left join (select distinct m.card_id
           from me_member_cards m
           where m.userID = 2 
           and m.status = 'Collecting'
          ) m2
     ON m2.cardID = c.id 
left join me_decks d
     ON d.deckName = c.deckName 
WHERE c.deckName = 'Couples'

如果您需要me_member_cards表中的更多信息,您可以使用闪亮的新别名再次加入

答案 1 :(得分:0)

您想知道套牌中的每张卡片,特定用户是否拥有此卡片,如果有,请显示me_member_cards详细信息。问题是用户可以多次使用卡,但您仍然只想显示卡的一个结果行。因此,在这种情况下,您必须决定显示哪些 me_member_cards数据。这是一个示例,其中显示每张卡的最大status

select *
from me_decks d
join me_cards c on c.deckname = d.deckname
left join
(
  select cardid, max(status) as max_status
  from me_member_cards
  where userid = 2 
  and status = 'Collecting'
  group by cardid
) mc on mc.cardid = c.id
where d.deckname = 'Couples';