4

我有三张桌子

让我们一个demo_organization ;

+--------------------+--------------+------+-----+---------+----------------+
| Field              | Type         | Null | Key | Default | Extra          |
+--------------------+--------------+------+-----+---------+----------------+
| id                 | int(11)      | NO   | PRI | NULL    | auto_increment |
| org_name           | varchar(100) | NO   |     | NULL    |                |
| org_type           | varchar(100) | NO   |     | NULL    |                |
| abn_acn_no         | varchar(100) | NO   |     | NULL    |                |
| org_url            | varchar(120) | NO   |     | NULL    |                |
| notes              | longtext     | NO   |     | NULL    |                |
| city               | varchar(100) | YES  |     | NULL    |                |

第二个是demo_user

mysql> desc demo_user ;
+--------------------+----------------+------+-----+---------+----------------+
| Field              | Type           | Null | Key | Default | Extra          |
+--------------------+----------------+------+-----+---------+----------------+
| id                 | int(11)        | NO   | PRI | NULL    | auto_increment |
| user_name          | varchar(100)   | NO   |     | NULL    |                |
| first_name         | varchar(100)   | NO   |     | NULL    |                |
| middle_name        | varchar(100)   | NO   |     | NULL    |                |
| last_name          | varchar(100)   | NO   |     | NULL    |                |
| image              | varchar(10000) | YES  |     | NULL    |                |
| password           | varchar(80)    | NO   |     | NULL    |                |
| role               | varchar(20)    | NO   |     | NULL    |                |
| org_name_id        | int(11)        | NO   | MUL | NULL    |                |
| timezone_id        | int(11)        | NO   | MUL | NULL    |                |

第三个是demo_meeting;外观

mysql> desc demo_meeting ;
+--------------------+--------------+------+-----+---------+----------------+
| Field              | Type         | Null | Key | Default | Extra          |
+--------------------+--------------+------+-----+---------+----------------+
| id                 | int(11)      | NO   | PRI | NULL    | auto_increment |
| name               | varchar(200) | NO   |     | NULL    |                |
| meetingID          | varchar(50)  | NO   |     | NULL    |                |
| venue_id           | int(11)      | YES  | MUL | NULL    |                |
| status             | int(11)      | YES  |     | NULL    |                |
| recurring_time     | varchar(50)  | NO   |     | NULL    |                |
| attendee_passwd    | varchar(100) | NO   |     | NULL    |                |
| moderator_passwd   | varchar(100) | NO   |     | NULL    |                |
| date_created       | datetime     | NO   |     | NULL    |                |
| start_time         | varchar(100) | NO   |     | NULL    |                |
| end_time           | varchar(100) | NO   |     | NULL    |                |
| meeting_duration   | varchar(100) | NO   |     | NULL    |                |
| meeting_datetime   | datetime     | YES  |     | NULL    |                |
| timezone           | varchar(50)  | NO   |     | NULL    |                |
| reminder           | tinyint(1)   | NO   |     | NULL    |                |
| duration           | varchar(20)  | NO   |     | NULL    |                |
| created_by_id      | int(11)      | NO   | MUL | NULL    |                |

第三张表中created_by_id指的是demo_user表的A用户(外键)

而 org_name_id(demo_user table ) 指的是 demo_organization (demo_organization table 的外键)

更新

mysql> desc demo_meetingroom;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| name       | varchar(100) | NO   |     | NULL    |                |
| type       | varchar(200) | NO   |     | NULL    |                |
| expired_on | varchar(100) | NO   |     | NULL    |                |
| user_id    | int(11)      | YES  | MUL | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

现在我正在尝试为特定组织安排所有会议。

我正在写一个查询

select meetingID ,type from demo_meeting as dm ,demo_meetingroom as dmr 
Where venue_id IS NOT NULL 
and dm.name = dmr.name 
AND created_by_id IN
  ( 
  SELECT id from demo_user WHERE org_name_id IN 
         (
         SELECT id from demo_organization where id =  
            (SELECT org_name_id from demo_user WHERE user_name = 'God') 
         )
   );

不幸的是它返回给我空集(但有价值)

请帮我看看我做错了什么?

4

2 回答 2

2

JOIN三个表而不是这些表IN。就像是:

SELECT
  dm.meetingID, 
  dm.type
FROM  demo_meeting dm
INNER JOIN demo_user         u   ON dm.created_by_id = u.id
INNER JOIN demo_organization org ON u.org_name_id = org.id
WHERE u.user_name = 'God'
 AND  dm.venue_id IS NOT NULL
于 2012-10-10T07:30:32.870 回答
1

如果可以使用会更好JOIN

SELECT  a.meetingID, d.type
FROM    demo_meeting a
        LEFT JOIN demo_user b
            ON a.created_by_id = b.id
        LEFT JOIN demo_organization c
            ON b.org_name_id = c.id
        LEFT JOIN demo_meetingroom d
            ON a.name = d.name
WHERE   a.venue_id IS NOT NULL AND
        b.username = 'GOD'
于 2012-10-10T07:31:18.317 回答