为什么不起作用?一个简单的SQL查询

时间:2011-12-05 07:21:49

标签: sql oracle oracle11g

我是SQL新手,我的数据库中有3个表:

  • 一个名为notifications
  • 其他notifications_log
  • ,第三个是control

notificationnotification_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

3 个答案:

答案 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
相关问题