简体   繁体   中英

Get data from table B in not exist

Hi guys im working on a problem. i have 2 tables, table a contains a list of users table b contains a list of their "clock ins", at the moment im selecting distinct dates from table b and then for each date, im checking if the user has checked in for that date. BUT what i want to be able to do is select some data from the Table B as well .. but when i try i just get spammed with all the data from table B. here is my statement.

SELECT *
FROM Table A,
     Table B
WHERE EXISTS (SELECT * 
              FROM Table B
              WHERE Table B.user_id = Table A.id 
                And Table B.date = 'given date here')

I still don't understand why you want to select data that is not there, but if you still have to, then I would take your schema as-

USER
-----------------
user_id
username
...
...


USER_CLOCKIN
-----------------
user_id
clockin_datetime
...
...

I would query this as-

SELECT u.*, uc.*
  FROM user u LEFT OUTER JOIN user_clockin uc
       ON u.user_id = uc.user_id
 WHERE uc.user_id IS NULL
;

This selects all the columns from both the tables. Modify your SELECT list as per your requirement.

Try running following:

SELECT table_a.name,table_b.date FROM table_a LEFT JOIN table_b ON table_a.id=table_b.id WHERE table_b.date=<xx.yy.zzzz>

of course replace xx.yy.zzzz with date wanted. also, visit http://www.mysql.org go to documentation, and find more info on join, left join and right join...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM