MySQL中的SELECT问题

时间:2013-09-08 11:21:12

标签: mysql select

我正在尝试执行SELECT,但我无法检索到我想要的数据。

我尝试了很多句子而没有成功,所以这就是我在这里揭露整个问题的原因。

这是我的数据库(四个表):

-- Table users
CREATE TABLE users (
    user_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    device_id VARCHAR(15),
    private_user_id VARCHAR(16),
    public_user_id VARCHAR(16)
);

-- Table events
CREATE TABLE events (
    event_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    public_event_id VARCHAR(8),
    ref_user_id INT NOT NULL REFERENCES users(user_id),
    name VARCHAR(64),
    description VARCHAR(256)
);

-- Table proposals
CREATE TABLE proposals (
    proposal_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    public_proposal_id VARCHAR(8),
    ref_event_id INT NOT NULL REFERENCES events(event_id),
    proposal_date VARCHAR(32),
    proposal_location VARCHAR(64),
    proposal_type INT
);

-- Table responses
CREATE TABLE responses (
    response_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    ref_user_id INT NOT NULL REFERENCES users(user_id),
    ref_proposal_id INT NOT NULL REFERENCES proposals(proposal_id),
    user_name VARCHAR(32),
    response INT
);

有用户。有活动。事件由一个用户创建。活动有提案。提案有来自用户的回复。我需要:

  

我需要在私有用户ID的情况下实现此目的。是这样的:给定   私有用户ID,分析该用户的响应,进行分析   这些回应的提议,最后得到了那些事件   回复,返回每个事件的创建者的公共用户ID。

我尝试过:

SELECT DISTINCT events.ref_user_id, public_event_id, name, description, public_user_id
FROM events, users, proposals, responses
WHERE responses.ref_user_id = user_id
  AND private_user_id = 'lk0X3lOQl972U7pB'

但检索到的public_user_id是已响应的用户,而我需要事件创建者。

2 个答案:

答案 0 :(得分:3)

SELECT ALL                                    -- show all
    u.pulic_user_id AS creator_pulic_user_id, -- creators' public IDs
    e.name          AS event_name             -- and events' names
FROM                                          -- from
    events AS e                               -- events
  JOIN                                        -- and
    users AS u                                -- their creator 
      ON e.ref_user_id = u.user_id
WHERE                                         -- where
    EXISTS                                    -- exists
    ( SELECT 1                                -- a
      FROM responses AS r                     -- response
        JOIN users AS ur                      -- by a user,
          ON ur.user_id = r.ref_user_id
        JOIN proposals AS p                   -- for a proposal
          ON p.proposal_id = r.ref_proposal_id 
      WHERE p.ref_event_id = e.event_id       -- that is about that event
        AND ur.private_user_id = @X           -- and the responding user  
    ) ;                                       -- has private ID = @X

Sidenote :内联外键约束,如下所示:

ref_user_id INT NOT NULL REFERENCES users(user_id),
在MySQL中解析

(仅用于语法正确性)和忽略。您必须在CREATE TABLE语句的末尾添加它们才能生效并执行。如果您已在表格中插入数据,请使用ALTER TABLE

答案 1 :(得分:0)

如果它只是错误的ID,那是因为你要从用户表中返回public_user_id,你需要events.ref_user_id,试试这个:

SELECT DISTINCT events.ref_user_id AS public_user_id , public_event_id, name, description
FROM events, users, proposals, responses 
WHERE (responses.ref_user_id = user_id 
  AND ref_proposal_id = proposal_id AND ref_event_id = event_id)

修改

我认为这就是你想要的:

SELECT DISTINCT e.ref_user_id AS public_user_id 
FROM events e, users u, proposals p, responses r
WHERE r.ref_user_id = u.user_id 
  AND r.ref_proposal_id = p.proposal_id 
  AND p.ref_event_id = e.event_id
  AND u.private_user_id = 'lk0X3lOQl972U7pB'
;