Idro February 2016

MySQL execute query only if a statement is verified

I have this query working well:

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, 
            

Answers


Paolof76 February 2016

I'm not saying anything about this database design... ;)

Why don't you just add a filter in the inner query like this:

1) First one to obtain the same information but only for that Disciplina ='yes' at London 2012

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  

Post Status

Asked in February 2016
Viewed 3,829 times
Voted 5
Answered 1 times

Search




Leave an answer