我是SQL新手,我的数据库中有3个表:
notifications
,notifications_log
,control
。 notification
和notification_log
都有notification_id
与pk-fk的关系。
control_id
中还有一个名为notifications
的列,我在control
表中也有此列。
现在我要做的是通过将description
表和notification_log
表连接到notification
字段来获取control
表的control_id
列下的行。你能帮帮我吗?
这是我尝试过的东西:
select c.control_name
from notifications note, notifications_log note_log, control c
where note_log.ALARM_ID = note.ALARM_ID
and note.CONTROL_ID = C.CONTROL_ID
order by control_name desc
答案 0 :(得分:2)
使用JOIN:
SELECT c.control_name, note_log.description FROM notifications note
INNER JOIN notifications_log note_log
ON note.notification_id = note_log.notification_id
INNER JOIN control c
ON note.control_id = c.control_id
ORDER BY c.control_name DESC
您必须了解的是控制表中的每个记录是否在其他表中都有相应的记录,否则将不会显示INNER JOIN。
答案 1 :(得分:0)
从notification_log中选择说明,其中notification_id(从通知中选择notification_id)
如果你只想获得描述..
希望它有所帮助:D
答案 2 :(得分:0)
从您的描述中,听起来好像您在查询中选择了错误的字段。尝试:
select distinct note_log.description
from notifications note, notifications_log note_log, control c
where note_log.notification_id = note.notification_id
and note.CONTROL_ID = C.CONTROL_ID
order by 1