SQL IF/EXISTS Statement -


i have 2 tables this..

table_gb:

     id:     author:        email:             login:     1.      john doe       john@email.com     1     2.      jenn smith     jenn@email.net     1     3.      michael        mich@email.co.uk   0 


table_user:

     id:     username:      email:     1.      jenstar        jenn@email.net     2.      knoxvile       knox@email.org     3.      johndoe        john@email.com` 


wanted make sql statement pulls fields table_gb , if "login" == 1 want go ahead , pull username matching email table_user...

returning this..

     id:     author:       username:      email:               login:     1.      john doe      johndoe        john@email.com       1     2.      jenn smith    jenstar        jenn@email.net       1     3.      michael                      mich@email.co.uk     0 


gets username table_user if login set 1 idea? have tried nothing seemed work!...

simply use left join

   select g.id, g.author, u.username, g.email, g.login      table_gb g  left join table_user u        on g.email = u.email; 

output:

╔════╦════════════╦══════════╦══════════════════╦═══════╗ ║ id ║   author   ║ username ║      email       ║ login ║ ╠════╬════════════╬══════════╬══════════════════╬═══════╣ ║  1 ║ john doe   ║ johndoe  ║ john@email.com   ║     1 ║ ║  2 ║ jenn smith ║ jenstar  ║ jenn@email.net   ║     1 ║ ║  3 ║ michael    ║ (null)   ║ mich@email.co.uk ║     0 ║ ╚════╩════════════╩══════════╩══════════════════╩═══════╝ 

see sqlfiddle


Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -