在Sql Join语句中苦苦挣扎。从DIfferent Tables中获取值

时间:2017-07-04 11:58:23

标签: mysql join left-join

我有三个mysql数据库表; scheduled_messagessubscribed_servicessubscription_services_msisdn

subscribed_services包含用户名和密码列。此表的id =主键和其他两个表的外键。

scheduled_messages表有列消息,scheduled_at和subscribed_services_id,它是表subscribed_services的外键。

subscription_services_msisdn表有phone_number和subscribed_services_id,它是表subscribed_services的外键。

我需要一个sql语句,可以获取消息 FROM scheduled_messages WHERE scheduled_at = CURDATE()和

phone_number FROM subscription_services_msisdn WHERE subscribed_services_id =" SELECT subscribed_services_id FROM scheduled_messages WHERE scheduled_at = CURDATE()"

最后

用户名密码 FROM subscribed_services WHERE id =" SELECT subscribed_services_id FROM scheduled_messages WHERE scheduled_at = CURDATE()"

我想出了这个加入声明但是没有用。即使表上有结果,我也得到0结果。

SELECT message, phone_number, username, password from scheduled_messages 
inner join 
subscribed_services on scheduled_messages.id = subscribed_services.id 
inner join 
subscription_services_msisdn on scheduled_messages.subscribed_services_id = 
subscription_services_msisdn.subscribed_services_id where scheduled_messages.scheduled_at = CURDATE();

示例数据

from subscribed_services;
+----+---------+-----------+--------+--------------+-----------+---------------------+
| id | keyword | shortcode | org_id | username     | password  | created_at          | 
+----+---------+-----------+--------+--------------+-----------+---------------------+
|  1 | Lawyer  | 21348     | 4      | Lawyer101API | qweasd123 | 2017-07-04 11:34:06 | 



from scheduled_messages;
+----+---------------------------+------------------------+---------------------+---------------------+
| id | message                   | subscribed_services_id | scheduled_at        | created_at          |       
+----+---------------------------+------------------------+---------------------+---------------------+
|  1 |  He also saw a poor widow |                      1 | 2017-07-04 11:32:52 | 2017-07-03 18:09:25 |
+----+---------------------------+------------------------+---------------------+---------------------+



from subscription_services_msisdn;
+----+--------------+------------------------+---------------------+---------------------+
| id | phone_number | subscribed_services_id | created_at          | updated_at          |
+----+--------------+------------------------+---------------------+---------------------+
|  1 | 224718488444 |                      1 | 2017-07-03 17:57:33 | 2017-07-03 17:57:33 |
|  2 | 224717746565 |                      1 | 2017-07-03 17:58:41 | 2017-07-03 17:58:41 |
|  3 | 224718036864 |                      1 | 2017-07-04 11:32:52 | 2017-07-04 11:32:52 |

如何获取当前日期安排的消息的用户名,密码,phone_number和消息?

有人帮忙吗?谢谢。

1 个答案:

答案 0 :(得分:0)

我看到的方式,根据提供的架构描述,您的连接条件有误。如果你得到正确的结果,你可以尝试这个并告诉我吗?

SELECT 
b.message, c.phone_number, a.username, a.password 
from 
subscribed_services a
inner join
scheduled_messages b on a.id = b.subscribed_services_id
inner join 
subscription_services_msisdn c on a.id = c.subscribed_services_id 
where DATE(b.scheduled_at) = CURDATE();