#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    2
    Rep Power
    0

    Question Using Join for three tables


    Hello friends,

    I have this scenario in my database:

    Table name: demo_candidatos (options to vote list)


    Table name: demo_voting (voters list)


    Table name: demo_registro (where those who already voted are registered)


    Then i'm using this query to select options from demo_candidatos table to get options that are not registered:

    SELECT C.`demo_eleccion_ideleccion` AS `Ideleccion`,
    C.`idcandidatos` AS `Idcandidatos`,
    C.`nombre` AS `Candidatos`,
    C.`numero` AS `Numero_Tarjeton`,
    C.`tarjeton` AS `tarjeton`
    FROM demo_candidatos C
    JOIN demo_registro t2
    ON t2.urna = C.demo_eleccion_ideleccion
    LEFT JOIN demo_voting t3
    ON t3.codigo = t2.codigo_votante
    WHERE t2.urna IS NULL

    Then, the voting options (demo_candidatos) must be loaded based on the row (codigo.demo_voting) = 333333, it should not be available for row 111111; because it is registered in the demo_registro table with the record (urna.demo_registro = 19 - 20 - 21) and (codigo_votante.demo_registro = 111111 - 111111 - 1111111)

    It's possible using Joins?

    Thank you!
  2. #2
  3. SQL Consultant
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2003
    Location
    Toronto Canada
    Posts
    27,774
    Rep Power
    4289
    if you really want WHERE t2.urna IS NULL then you have to make that join LEFT OUTER as well
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Posts
    2
    Rep Power
    0
    Ok, then talk me where i have to put LEFT OUTER?


    Regards!
  6. #4
  7. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,229
    Rep Power
    4149
    You change your join for t2. So
    Code:
    JOIN demo_registro t2
    becomes
    Code:
    LEFT JOIN demo_registro t2
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo