只有在验证语句时,MySQL才会执行查询

时间:2016-02-08 09:28:10

标签: mysql

我的查询效果很好:

SELECT
    Disciplina,
    Categoria,
    IF (MAX(`London 2012`) > 0, 'yes', 'no') AS 'London 2012',
    IF (MAX(`Beijing 2008`) > 0, 'yes', 'no') AS 'Beijing 2008',
    IF (MAX(`Athens 2004`) > 0, 'yes', 'no') AS 'Athens 2004',
    IF (MAX(`Sydney 2000`) > 0, 'yes', 'no') AS 'Sydney 2000',
    IF (MAX(`Atlanta 1996`) > 0, 'yes', 'no') AS 'Atlanta 1996',
    IF (MAX(`Barcelona 1992`) > 0, 'yes', 'no') AS 'Barcelona 1992',
    IF (MAX(`Seoul 1988`) > 0, 'yes', 'no') AS 'Seoul 1988',
    IF (MAX(`Los Angeles 1984`) > 0, 'yes', 'no') AS 'Los Angeles 1984',
    IF (MAX(`Moscow 1980`) > 0, 'yes', 'no') AS 'Moscow 1980',
    IF (MAX(`Montreal 1976`) > 0, 'yes', 'no') AS 'Montreal 1976',
    IF (MAX(`Munich 1972`) > 0, 'yes', 'no') AS 'Munich 1972',
    IF (MAX(`Mexico 1968`) > 0, 'yes', 'no') AS 'Mexico 1968',
    IF (MAX(`Tokyo 1964`) > 0, 'yes', 'no') AS 'Tokyo 1964',
    IF (MAX(`Rome 1960`) > 0, 'yes', 'no') AS 'Rome 1960',
    IF (MAX(`Melbourne / Stockholm 1956`) > 0, 'yes', 'no') AS 'Melbourne / Stockholm 1956',
    IF (MAX(`Helsinki 1952`) > 0, 'yes', 'no') AS 'Helsinki 1952',
    IF (MAX(`London 1948`) > 0, 'yes', 'no') AS 'London 1948',
    IF (MAX(`Berlin 1936`) > 0, 'yes', 'no') AS 'Berlin 1936',
    IF (MAX(`Los Angeles 1932`) > 0, 'yes', 'no') AS 'Los Angeles 1932',
    IF (MAX(`Amsterdam 1928`) > 0, 'yes', 'no') AS 'Amsterdam 1928',
    IF (MAX(`Paris 1924`) > 0, 'yes', 'no') AS 'Paris 1924',
    IF (MAX(`Antwerp 1920`) > 0, 'yes', 'no') AS 'Antwerp 1920',
    IF (MAX(`Stockholm 1912`) > 0, 'yes', 'no') AS 'Stockholm 1912',
    IF (MAX(`London 1908`) > 0, 'yes', 'no') AS 'London 1908',
    IF (MAX(`St Louis 1904`) > 0, 'yes', 'no') AS 'St Louis 1904',
    IF (MAX(`Paris 1900`) > 0, 'yes', 'no') AS 'Paris 1900',
    IF (MAX(`Athens 1896`) > 0, 'yes', 'no') AS 'Athens 1896'
FROM
    (
        SELECT 
            Disciplina, 
            Categoria, 
            IF (Olimpiade = 'London 2012', 1, 0) AS 'London 2012',
            IF (Olimpiade = 'Beijing 2008', 1, 0) AS 'Beijing 2008',
            IF (Olimpiade = 'Athens 2004', 1, 0) AS 'Athens 2004',
            IF (Olimpiade = 'Sydney 2000', 1, 0) AS 'Sydney 2000',
            IF (Olimpiade = 'Atlanta 1996', 1, 0) AS 'Atlanta 1996',
            IF (Olimpiade = 'Barcelona 1992', 1, 0) AS 'Barcelona 1992',
            IF (Olimpiade = 'Seoul 1988', 1, 0) AS 'Seoul 1988',
            IF (Olimpiade = 'Los Angeles 1984', 1, 0) AS 'Los Angeles 1984',
            IF (Olimpiade = 'Moscow 1980', 1, 0) AS 'Moscow 1980',
            IF (Olimpiade = 'Montreal 1976', 1, 0) AS 'Montreal 1976',
            IF (Olimpiade = 'Munich 1972', 1, 0) AS 'Munich 1972',
            IF (Olimpiade = 'Mexico 1968', 1, 0) AS 'Mexico 1968',
            IF (Olimpiade = 'Tokyo 1964', 1, 0) AS 'Tokyo 1964',
            IF (Olimpiade = 'Rome 1960', 1, 0) AS 'Rome 1960',
            IF (Olimpiade = 'Melbourne / Stockholm 1956', 1, 0) AS 'Melbourne / Stockholm 1956',
            IF (Olimpiade = 'Helsinki 1952', 1, 0) AS 'Helsinki 1952',
            IF (Olimpiade = 'London 1948', 1, 0) AS 'London 1948',
            IF (Olimpiade = 'Berlin 1936', 1, 0) AS 'Berlin 1936',
            IF (Olimpiade = 'Los Angeles 1932', 1, 0) AS 'Los Angeles 1932',
            IF (Olimpiade = 'Amsterdam 1928', 1, 0) AS 'Amsterdam 1928',
            IF (Olimpiade = 'Paris 1924', 1, 0) AS 'Paris 1924',
            IF (Olimpiade = 'Antwerp 1920', 1, 0) AS 'Antwerp 1920',
            IF (Olimpiade = 'Stockholm 1912', 1, 0) AS 'Stockholm 1912',
            IF (Olimpiade = 'London 1908', 1, 0) AS 'London 1908',
            IF (Olimpiade = 'St Louis 1904', 1, 0) AS 'St Louis 1904',
            IF (Olimpiade = 'Paris 1900', 1, 0) AS 'Paris 1900',
            IF (Olimpiade = 'Athens 1896', 1, 0) AS 'Athens 1896'
        FROM SummerTotalMedals
        WHERE Sport='Athletics'
    ) AS Games
GROUP BY Disciplina, Categoria
ORDER BY Disciplina ASC, Categoria ASC

我想从这个单一查询中获得两种类型的查询。

1)第一个获得相同的信息,但仅限2012年伦敦的Disciplina ='yes'

2)第二个在2012年伦敦获得相同的Disciplina ='no'信息。

我可以这样做吗?我试过自己但效果不佳。

1 个答案:

答案 0 :(得分:0)

我对此数据库设计没有说什么......;)

为什么不在内部查询中添加一个过滤器,如下所示:

1)第一个获得相同的信息,但仅限2012年伦敦的Disciplina ='yes'

SELECT
    Disciplina,
    Categoria,
    IF (MAX(`London 2012`) > 0, 'yes', 'no') AS 'London 2012',
    IF (MAX(`Beijing 2008`) > 0, 'yes', 'no') AS 'Beijing 2008',
    IF (MAX(`Athens 2004`) > 0, 'yes', 'no') AS 'Athens 2004',
    IF (MAX(`Sydney 2000`) > 0, 'yes', 'no') AS 'Sydney 2000',
    IF (MAX(`Atlanta 1996`) > 0, 'yes', 'no') AS 'Atlanta 1996',
    IF (MAX(`Barcelona 1992`) > 0, 'yes', 'no') AS 'Barcelona 1992',
    IF (MAX(`Seoul 1988`) > 0, 'yes', 'no') AS 'Seoul 1988',
    IF (MAX(`Los Angeles 1984`) > 0, 'yes', 'no') AS 'Los Angeles 1984',
    IF (MAX(`Moscow 1980`) > 0, 'yes', 'no') AS 'Moscow 1980',
    IF (MAX(`Montreal 1976`) > 0, 'yes', 'no') AS 'Montreal 1976',
    IF (MAX(`Munich 1972`) > 0, 'yes', 'no') AS 'Munich 1972',
    IF (MAX(`Mexico 1968`) > 0, 'yes', 'no') AS 'Mexico 1968',
    IF (MAX(`Tokyo 1964`) > 0, 'yes', 'no') AS 'Tokyo 1964',
    IF (MAX(`Rome 1960`) > 0, 'yes', 'no') AS 'Rome 1960',
    IF (MAX(`Melbourne / Stockholm 1956`) > 0, 'yes', 'no') AS 'Melbourne / Stockholm 1956',
    IF (MAX(`Helsinki 1952`) > 0, 'yes', 'no') AS 'Helsinki 1952',
    IF (MAX(`London 1948`) > 0, 'yes', 'no') AS 'London 1948',
    IF (MAX(`Berlin 1936`) > 0, 'yes', 'no') AS 'Berlin 1936',
    IF (MAX(`Los Angeles 1932`) > 0, 'yes', 'no') AS 'Los Angeles 1932',
    IF (MAX(`Amsterdam 1928`) > 0, 'yes', 'no') AS 'Amsterdam 1928',
    IF (MAX(`Paris 1924`) > 0, 'yes', 'no') AS 'Paris 1924',
    IF (MAX(`Antwerp 1920`) > 0, 'yes', 'no') AS 'Antwerp 1920',
    IF (MAX(`Stockholm 1912`) > 0, 'yes', 'no') AS 'Stockholm 1912',
    IF (MAX(`London 1908`) > 0, 'yes', 'no') AS 'London 1908',
    IF (MAX(`St Louis 1904`) > 0, 'yes', 'no') AS 'St Louis 1904',
    IF (MAX(`Paris 1900`) > 0, 'yes', 'no') AS 'Paris 1900',
    IF (MAX(`Athens 1896`) > 0, 'yes', 'no') AS 'Athens 1896'
FROM
    (
        SELECT 
            Disciplina, 
            Categoria, 
            IF (Olimpiade = 'London 2012', 1, 0) AS 'London 2012',
            IF (Olimpiade = 'Beijing 2008', 1, 0) AS 'Beijing 2008',
            IF (Olimpiade = 'Athens 2004', 1, 0) AS 'Athens 2004',
            IF (Olimpiade = 'Sydney 2000', 1, 0) AS 'Sydney 2000',
            IF (Olimpiade = 'Atlanta 1996', 1, 0) AS 'Atlanta 1996',
            IF (Olimpiade = 'Barcelona 1992', 1, 0) AS 'Barcelona 1992',
            IF (Olimpiade = 'Seoul 1988', 1, 0) AS 'Seoul 1988',
            IF (Olimpiade = 'Los Angeles 1984', 1, 0) AS 'Los Angeles 1984',
            IF (Olimpiade = 'Moscow 1980', 1, 0) AS 'Moscow 1980',
            IF (Olimpiade = 'Montreal 1976', 1, 0) AS 'Montreal 1976',
            IF (Olimpiade = 'Munich 1972', 1, 0) AS 'Munich 1972',
            IF (Olimpiade = 'Mexico 1968', 1, 0) AS 'Mexico 1968',
            IF (Olimpiade = 'Tokyo 1964', 1, 0) AS 'Tokyo 1964',
            IF (Olimpiade = 'Rome 1960', 1, 0) AS 'Rome 1960',
            IF (Olimpiade = 'Melbourne / Stockholm 1956', 1, 0) AS 'Melbourne / Stockholm 1956',
            IF (Olimpiade = 'Helsinki 1952', 1, 0) AS 'Helsinki 1952',
            IF (Olimpiade = 'London 1948', 1, 0) AS 'London 1948',
            IF (Olimpiade = 'Berlin 1936', 1, 0) AS 'Berlin 1936',
            IF (Olimpiade = 'Los Angeles 1932', 1, 0) AS 'Los Angeles 1932',
            IF (Olimpiade = 'Amsterdam 1928', 1, 0) AS 'Amsterdam 1928',
            IF (Olimpiade = 'Paris 1924', 1, 0) AS 'Paris 1924',
            IF (Olimpiade = 'Antwerp 1920', 1, 0) AS 'Antwerp 1920',
            IF (Olimpiade = 'Stockholm 1912', 1, 0) AS 'Stockholm 1912',
            IF (Olimpiade = 'London 1908', 1, 0) AS 'London 1908',
            IF (Olimpiade = 'St Louis 1904', 1, 0) AS 'St Louis 1904',
            IF (Olimpiade = 'Paris 1900', 1, 0) AS 'Paris 1900',
            IF (Olimpiade = 'Athens 1896', 1, 0) AS 'Athens 1896'
        FROM SummerTotalMedals
        WHERE Sport='Athletics'
        AND Olimpiade = 'London 2012' AND Disciplina='yes'
    ) AS Games
GROUP BY Disciplina, Categoria
ORDER BY Disciplina ASC, Categoria ASC

第二,你只需要改变Disciplina ='no'。 这是你所期待的,我不理解这个问题吗?