1

I am trying to perform a SELECT but I am not able to retrieve the data I want.

I have tried lots of sentences without success, so that's why I am exposing here the whole problem.

Here is my database (four tables):

-- 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
);

There are users. There are events. Events are created by one user. Events have proposals. Proposals have responses from the users. I need:

I need to achieve this given a private user id. Is something like: given the private user id, analyze the responses of that user, so analyze the proposals of that responses, and finally get the events of that responses, RETURNING THE PUBLIC USER ID of the creator of each event.

I have tried with:

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'

but the public_user_id retrieved is the user that has responded, while I need the event creator.

4

2 回答 2

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

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

ref_user_id INT NOT NULL REFERENCES users(user_id),

被解析(仅用于语法正确性)并在 MySQL 中被忽略。您必须在CREATE TABLE声明的末尾添加它们才能有效和强制执行。或者ALTER TABLE,如果您已经在表中插入了数据,请使用。

于 2013-09-08T11:49:59.463 回答
0

如果只是 id 错误,那是因为您要从 user 表中返回 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'
;
于 2013-09-08T11:26:43.097 回答