mysql与条件的一对多关系

时间:2012-03-30 07:04:47

标签: php mysql

我有2个表事件和events_visibility

事件表和列

id - title - all

events_visibility及其专栏

id - event_id - visibile_to - visibile_to_id

这里使用visibile_to谁可以看到这个事件。这里可能是A组,B组,C组,因此一个事件可以在event_visibility中有多个条目

SELECT Event.*, EventVisibility.* FROM events AS Event 
LEFT JOIN events_visibility AS EventVisibility
ON 
  (Event.id = EventVisibility.event_id) 
WHERE 
 'Event.all' = 1 
 OR 
  (
    'EventVisibility.visible_to_id' = 2 AND 
    'EventVisibility.visible_to' = 159
 )

我想根据可见性获取唯一的事件,如果全部,这样的话 这个查询没有给我正确的结果。

你能看一下吗?

1 个答案:

答案 0 :(得分:0)

也许你在单引号内写了专栏。试试这个

   SELECT Event.*, EventVisibility.* FROM events AS Event 
    LEFT JOIN events_visibility AS EventVisibility
   ON 
   (Event.id = EventVisibility.event_id) 
   WHERE 
   Event.all = 1 
   OR 
   (
    EventVisibility.visible_to_id = 2 AND 
    EventVisibility.visible_to = 159
    )