如何使用联接从两个不同的表中选择两列

时间:2019-04-29 07:59:23

标签: mysql join

问题陈述:-从两个表中获取记录,一个是出勤率,第二个是关于特定用户的访问以及在特定日期之间的访问

我有两个表,一个是出勤率,第二是出访率

1)出席

+-------------+------------+---------------------+
| user        | date_today | in_time             |
+-------------+------------+---------------------+
| MCCuser0036 | 25-02-2019 | 25-02-2019 10:59:08 |
| MCCuser0036 | 12-03-2019 | 12-03-2019 10:59:54 |
| MCCuser0036 | 14-03-2019 | 14-03-2019 11:01:11 |
| MCCuser0036 | 25-01-2019 | 25-01-2019 11:02:09 |
| MCCuser0036 | 25-03-2019 | 25-03-2019 11:03:10 |
| MCCuser0035 | 25-03-2019 | 25-03-2019 11:39:38 |
| MCCuser0036 | 26-03-2019 | 26-03-2019 11:30:47 |
| MCCuser0036 | 27-03-2019 | 27-03-2019 10:36:30 |
| MCCuser0036 | 28-03-2019 | 28-03-2019 17:18:01 |
| MCCuser0036 | 01-04-2019 | 01-04-2019 15:29:08 |
| MCCuser0036 | 08-04-2019 | 08-04-2019 23:01:57 |
| MCCuser007  | 26-04-2019 | 26-04-2019 20:04:36 |
| MCCuser0036 | 27-04-2019 | 27-04-2019 21:06:17 |
+-------------+------------+---------------------+

2)访问

+-------------+------------+---------------------+
| username    | add_time   | visit_time_in       |
+-------------+------------+---------------------+
| MCCuser0036 | 13-02-2019 | 13-02-2019 11:46:55 |
| MCCuser0036 | 14-02-2019 | 14-02-2019 11:47:04 |
| MCCuser0036 | 14-02-2019 | 14-02-2019 11:51:07 |
| MCCuser0036 | 14-02-2019 | 14-02-2019 11:51:30 |
| MCCuser0036 | 14-02-2019 | 14-02-2019 11:51:36 |
| MCCuser0036 | 18-02-2019 | 18-02-2019 15:09:42 |
| MCCuser0036 | 20-02-2019 | 20-02-2019 12:11:30 |
| MCCuser0036 | 25-02-2019 | 25-02-2019 13:05:42 |
| MCCuser0036 | 11-03-2019 | 11-03-2019 12:48:28 |
| MCCuser0036 | 01-04-2019 | 01-04-2019 15:55:45 |
| MCCuser007  | 26-04-2019 | 26-04-2019 20:04:55 |
| MCCuser007  | 26-04-2019 | 26-04-2019 20:05:10 |
| MCCuser0036 | 27-04-2019 | 27-04-2019 21:06:55 |
+-------------+------------+---------------------+

我想要的是按给定的日期范围按特定顺序记录特定用户的出勤和访问。

我的输出应该像是第一次出席,然后是当天该用户的所有访问

+----------+------------------------+----------+---------------------+
|user      |in_time                 |username  |visit_time_in        |
+----------+------------------------+----------+---------------------+
|given user|attendence of given date|given user|visits of given user |
+----------+------------------------+----------+---------------------+

我正在使用此查询,但它给了我一个空的设置和警告,我对mysql和联接没有任何深入的了解,所以我不知道即时消息是否以正确的方式进行操作

SELECT a.user,a.in_time,v.username,v.visit_time_in 
   FROM attendence AS a 
   INNER JOIN visits AS v ON a.user=v.username 
      WHERE a.user='MCCuser0036' 
      AND a.in_time BETWEEN STR_TO_DATE('01-04-2019', '%d-%m-%Y') 
      AND STR_TO_DATE('27-04-2019', '%d-%m-%Y') 
   ORDER BY a.in_time,v.visit_time_in DESC; 

Empty SET, 22 WARNINGS (0.00 sec) 

注意:没有主键和外键以及date_today,in_time和add_time,visit_time_in以及user,username列均为varchar

我想要的是@SebastianBrosch的

更新后的编辑答案,但是我的数据库中有更多列,因此在获得基本结构后,我向其中添加了一些列并进行查询

SELECT * FROM (SELECT 'attendence' AS 'type', user AS 'username', STR_TO_DATE(in_time, '%d-%m-%Y %H:%i:%s') AS 'time_in', STR_TO_DATE(out_time, '%d-%m-%Y %H:%i:%s') AS 'time_out', location_in as 'place_in' FROM attendence UNION ALL SELECT 'visit', username, STR_TO_DATE(visit_time_in, '%d-%m-%Y %H:%i:%s'), STR_TO_DATE(visit_time_out, '%d-%m-%Y %H:%i:%s'), in_address FROM visits) t WHERE t.username = 'MCCuser0036' AND time_in BETWEEN STR_TO_DATE('01-01-2019', '%d-%m-%Y %H:%i:%s') AND STR_TO_DATE('27-04-2019', '%d-%m-%Y %H:%i:%s') ORDER BY time_in ASC;

但这给了我意外的结果

+------------+-------------+---------------------+---------------------+----------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| type       | username    | time_in             | time_out            | address_in                                                                                                           | address_out                                                                                         |
+------------+-------------+---------------------+---------------------+----------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+
| attendence | MCCuser0036 | 2019-01-25 11:02:09 | 2019-01-25 11:02:10 | in_address                                                                                                           | out_address                                                                                         |
| visit      | MCCuser0036 | 2019-02-13 11:46:55 | 2019-02-13 11:47:00 | Sewri - Chembur Rd, Trombay Indusrial Area, Antop Hill, Mumbai, Maharashtra 400037, India                            | Sewri - Chembur Rd, Trombay Indusrial Area, Antop Hill, Mumbai, Maharashtra 400037, India           |
| visit      | MCCuser0036 | 2019-02-14 11:47:04 | 2019-02-14 11:47:10 | Sewri - Chembur Rd, Trombay Indusrial Area, Antop Hill, Mumbai, Maharashtra 400037, India                            | Sewri - Chembur Rd, Trombay Indusrial Area, Antop Hill, Mumbai, Maharashtra 400037, India           |
| visit      | MCCuser0036 | 2019-02-14 11:51:07 | 2019-02-14 11:51:10 | Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India                                       | Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India                      |
| visit      | MCCuser0036 | 2019-02-14 11:51:30 | 2019-02-14 11:51:34 | Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India                                       | Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India                      |
| visit      | MCCuser0036 | 2019-02-14 11:51:36 | 2019-02-14 11:51:39 | Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India                                       | Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India                      |
| visit      | MCCuser0036 | 2019-02-18 15:09:42 | NULL                | Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India                                       |                                                                                                     |
| visit      | MCCuser0036 | 2019-02-20 12:11:30 | 2019-02-20 13:44:36 | Sewri - Chembur Rd, Trombay Indusrial Area, Antop Hill, Mumbai, Maharashtra 400037, India                            | Building No 57, Gaurav Harmony, CGS Colony, Sector 7, Antop Hill, Mumbai, Maharashtra 400037, India |
| attendence | MCCuser0036 | 2019-02-25 10:59:08 | 2019-02-25 10:59:16 | in_address                                                                                                           | out_address                                                                                         |
| visit      | MCCuser0036 | 2019-02-25 13:05:42 | NULL                | Kokri Agar Road, Koliwada, Kokri Agar, Sion, Mumbai, Maharashtra 400037, India                                       |                                                                                                     |
| visit      | MCCuser0036 | 2019-03-11 12:48:28 | NULL                | jay maharashNagar kokri agar S.M road anophill church Near By sayyed alam, musjid, Mumbai, Maharashtra 400037, India |                                                                                                     |
| attendence | MCCuser0036 | 2019-03-12 10:59:54 | 2019-03-12 11:00:24 | in_address                                                                                                           | out_address                                                                                         |
| attendence | MCCuser0036 | 2019-03-14 11:01:11 | 2019-03-14 11:01:28 | in_address                                                                                                           | out_address                                                                                         |
| attendence | MCCuser0036 | 2019-03-25 11:03:10 | 2019-03-25 11:16:12 | in_address                                                                                                           | out_address                                                                                         |
| attendence | MCCuser0036 | 2019-03-26 11:30:47 | NULL                | in_address                                                                                                           | out_address                                                                                         |
| attendence | MCCuser0036 | 2019-03-27 10:36:30 | 2019-03-27 10:49:05 | in_address                                                                                                           | out_address                                                                                         |
| attendence | MCCuser0036 | 2019-03-28 17:18:01 | NULL                | in_address                                                                                                           | out_address                                                                                         |
| attendence | MCCuser0036 | 2019-04-01 15:29:08 | NULL                | in_address                                                                                                           | out_address                                                                                         |
| visit      | MCCuser0036 | 2019-04-01 15:55:45 | NULL                | 736, Aala Hazrat Rd, Jagruti Nagar, Nehru Nagar, Kurla East, Mumbai, Maharashtra 400071, India                       |                                                                                                     |
| attendence | MCCuser0036 | 2019-04-11 15:17:56 | NULL                | in_address                                                                                                           | out_address                                                                                         |
| attendence | MCCuser0036 | 2019-04-23 17:41:05 | 2019-04-23 17:42:54 | in_address                                                                                                           | out_address                                                                                         |
+------------+-------------+---------------------+---------------------+----------------------------------------------------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------+

有人可以指出为什么吗?

2 个答案:

答案 0 :(得分:0)

select a.*,v.* from attendence as a
inner join visits as v on a.user = v.username

答案 1 :(得分:0)

您还必须在STR_TO_DATE列上使用a.in_time才能获得结果:

SELECT a.user, a.in_time, v.username, v.visit_time_in 
FROM attendence AS a INNER JOIN visits AS v ON a.user = v.username 
WHERE a.user = 'MCCuser0036' 
  AND STR_TO_DATE(a.in_time, '%d-%m-%Y') BETWEEN STR_TO_DATE('01-04-2019', '%d-%m-%Y') AND STR_TO_DATE('27-04-2019', '%d-%m-%Y')
ORDER BY STR_TO_DATE(a.in_time, '%d-%m-%Y'), STR_TO_DATE(v.visit_time_in, '%d-%m-%Y') DESC;

由于a.in_time BETWEEN str_to_date('01-04-2019', '%d-%m-%Y') and str_to_date('27-04-2019', '%d-%m-%Y')不是有效的日期值,因此条件a.in_time不能为真,因此不会得到结果。您还必须使用STR_TO_DATE上的ORDER BY来获取按日期值排序的记录。

注意:您应在DATETIMEin_time列上使用visit_time_in数据类型,并在{{1}上使用DATE数据类型}和add_time。因此,您无需先使用转换功能(例如date_today)就可以进行过滤和排序。


要通过一个查询仅获取两个表的行,可以使用STR_TO_DATE,如下所示:

UNION ALL