简体   繁体   English

从表B获取数据不存在

[英]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. 我有2个表,表a包含用户列表,表b包含其“ clock ins”列表,目前我从表b中选择了不同的日期,然后针对每个日期,im检查用户是否已签入该日期日期。 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. 但是我想做的是也从表B中选择一些数据..但是当我尝试使用表B中的所有数据时,我只会收到垃圾邮件。这是我的声明。

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. 根据您的要求修改您的SELECT列表。

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. 当然,将xx.yy.zzzz替换为想要的日期。 also, visit http://www.mysql.org go to documentation, and find more info on join, left join and right join... 另外,访问http://www.mysql.org转到文档,找到有关连接,左连接和右连接的更多信息...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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