从表中选择A列或B列中的值

时间:2016-06-25 16:40:02

标签: mysql sql

我正在制作电影数据库并且我有一个链接表,所以我可以将电影链接到续集。我已经写了下面的查询,所以我可以返回其中movie_id在链接表的任何列中的续集列表但是我想知道是否有更好的方法来执行此操作,因为它似乎是一个相当长的啰嗦怎么样的呢?

SELECT movie_id, movie_title FROM movies WHERE movie_id in 
    (SELECT movie_1 FROM lk_movies WHERE movie_1 = 1  or  movie_2 = 1 or  movie_3 = 1 or movie_4 = 1 or movie_5 = 1 or movie_6 = 1 or movie_7 = 1 or movie_8 = 1 or movie_9 = 1 or movie_10 = 1)
or movie_id in 
    (SELECT movie_2 FROM lk_movies WHERE movie_1 = 1  or  movie_2 = 1 or  movie_3 = 1 or movie_4 = 1 or movie_5 = 1 or movie_6 = 1 or movie_7 = 1 or movie_8 = 1 or movie_9 = 1 or movie_10 = 1)
or movie_id in 
    (SELECT movie_3 FROM lk_movies WHERE movie_1 = 1  or  movie_2 = 1 or  movie_3 = 1 or movie_4 = 1 or movie_5 = 1 or movie_6 = 1 or movie_7 = 1 or movie_8 = 1 or movie_9 = 1 or movie_10 = 1)
or movie_id in 
    (SELECT movie_4 FROM lk_movies WHERE movie_1 = 1  or  movie_2 = 1 or  movie_3 = 1 or movie_4 = 1 or movie_5 = 1 or movie_6 = 1 or movie_7 = 1 or movie_8 = 1 or movie_9 = 1 or movie_10 = 1)
or movie_id in 
    (SELECT movie_5 FROM lk_movies WHERE movie_1 = 1  or  movie_2 = 1 or  movie_3 = 1 or movie_4 = 1 or movie_5 = 1 or movie_6 = 1 or movie_7 = 1 or movie_8 = 1 or movie_9 = 1 or movie_10 = 1)
or movie_id in 
    (SELECT movie_6 FROM lk_movies WHERE movie_1 = 1  or  movie_2 = 1 or  movie_3 = 1 or movie_4 = 1 or movie_5 = 1 or movie_6 = 1 or movie_7 = 1 or movie_8 = 1 or movie_9 = 1 or movie_10 = 1)
or movie_id in 
    (SELECT movie_7 FROM lk_movies WHERE movie_1 = 1  or  movie_2 = 1 or  movie_3 = 1 or movie_4 = 1 or movie_5 = 1 or movie_6 = 1 or movie_7 = 1 or movie_8 = 1 or movie_9 = 1 or movie_10 = 1)
or movie_id in 
    (SELECT movie_8 FROM lk_movies WHERE movie_1 = 1  or  movie_2 = 1 or  movie_3 = 1 or movie_4 = 1 or movie_5 = 1 or movie_6 = 1 or movie_7 = 1 or movie_8 = 1 or movie_9 = 1 or movie_10 = 1)
or movie_id in 
    (SELECT movie_9 FROM lk_movies WHERE movie_1 = 1  or  movie_2 = 1 or  movie_3 = 1 or movie_4 = 1 or movie_5 = 1 or movie_6 = 1 or movie_7 = 1 or movie_8 = 1 or movie_9 = 1 or movie_10 = 1)
or movie_id in 
    (SELECT movie_10 FROM lk_movies WHERE movie_1 = 1  or  movie_2 = 1 or  movie_3 = 1 or movie_4 = 1 or movie_5 = 1 or movie_6 = 1 or movie_7 = 1 or movie_8 = 1 or movie_9 = 1 or movie_10 = 1);

2 个答案:

答案 0 :(得分:1)

不幸的是Mysql不支持UnpivotCross Apply运营商,所以这是我提出的最好的

子查询

SELECT movie_1 
FROM   lk_movies 
WHERE  movie_1 = 1 
        OR movie_2 = 1 
        OR movie_3 = 1 
        OR movie_4 = 1 
        OR movie_5 = 1 
        OR movie_6 = 1 
        OR movie_7 = 1 
        OR movie_8 = 1 
        OR movie_9 = 1 
        OR movie_10 = 1 
可以通过反转IN运算符

来重写

SELECT movie_10 
FROM   lk_movies 
WHERE  1 IN( movie_1, movie_2, movie_3, movie_4, 
             movie_5, movie_6, movie_7, movie_8, 
             movie_9, movie_10 ) 

然后使用Union all而不是多个OR条件,这是完整查询

 SELECT movie_id, 
       movie_title 
FROM   movies 
WHERE  movie_id IN (SELECT movie_1 
                    FROM   lk_movies 
                    WHERE  1 IN( movie_1, movie_2, movie_3, movie_4, 
                                 movie_5, movie_6, movie_7, movie_8, 
                                 movie_9, movie_10 ) 
                    UNION ALL 
                    SELECT movie_2 
                    FROM   lk_movies 
                    WHERE  1 IN( movie_1, movie_2, movie_3, movie_4, 
                                 movie_5, movie_6, movie_7, movie_8, 
                                 movie_9, movie_10 ) 
                    UNION ALL 
                    ...............
                    ...............
                    SELECT movie_10 
                    FROM   lk_movies 
                    WHERE  1 IN( movie_1, movie_2, movie_3, movie_4, 
                                 movie_5, movie_6, movie_7, movie_8, 
                                 movie_9, movie_10 )) 

答案 1 :(得分:1)

当您创建一个包含附加数字的列的表时,您几乎肯定做错了。您的表格不是normalized properly

以下所有代码均未经过测试,因为我不使用mysql。它可能有一些语法错误,但你应该明白这一点。

您可以创建如下表格:

CREATE TABLE Movie
| ID | Title   | ... | Sequel |
|  1 | Shrek   | ... | 2      |
|  2 | Shrek 2 | ... | 3      |
|  3 | Shrek 3 | ... | NULL   |

这样您只知道下一部续集,并且您必须关注指向下一个ID的Sequel列,直到您找到一张{{1}的电影} NULL

Sequel

要在一个查询中查找所有续集,请考虑使用单独的续集表。这也消除了续集数量的限制。

SELECT A.Titel, B.Titel AS Sequel
FROM Movie A
INNER JOIN Movie B
ON A.Sequel = B.ID

Movie
| ID | Title    | ... |
|  1 | Shrek    | ... |
|  2 | Shrek 2  | ... |
|  3 | Shrek 3  | ... |

然后你可以从中选择

Sequel
| Movie_ID | Sequel_ID |
|        1 |         2 | 
|        1 |         3 |
|        2 |         3 |

这里的缺点是你复制信息,即你​​需要为史莱克存储史莱克2和怪物史莱克3作为续集,而史莱克2你复制史莱克3是史莱克2续集的事实。当然,你总是只想从第一部电影中搜索。

注意汉兰达的角落案例,它不能有续集。只能有一个。