简体   繁体   中英

How can I write a MySQL query for this database design?

Task 1

First I have a specific user, I want to see with which user it is connected to. Let's say alex in following table. I want to check if alex is connected to another user? In following table it is connected with John and christina, so I select john and christina.

Task 2

Now I have another user. Let's say martin, I want to see which of the previous selected users (john and christina) are connected with the martin.

ID  user1      user2   status
1   alex       john      1
2   john       martin    1
3   Jane       smith     1
4   smith      john      1
5   christina  alex      1

For example, in above table:

People who are connected to alex are john and christina. We want to check which of the users (john and christina) are connected with martin, and the result should be row2.

Update

You all are mentioning that the database design is not good, can you please tell me what is wrong with it? Here is my user table:

Table 1, users

ID      username

and here is Table 2, user_connections

ConnectionID     user1_ID     user_Connectedto

I'm not sure your exact requirements but it would seem the database design you chose isn't really the best.

I think you would have wanted to go with a table with unique users. And then another table that links IDs of those users

--Users Table
ID     Name
1      Alex
2      John
3      Jane
...

--UserConnection Table
ID     ID_ConnectedTo
1      2
1      3

In this case, Alex is connected to John and Alex is connected to Jane.

Here's what you asked for, for you current schema:

SELECT user2 FROM
(SELECT user1, user2 FROM connections
UNION
SELECT user2, user1 FROM connections) t
WHERE user1 = 'martin'

You should consider the idea that "john connected with alex" isn't the same as "alex connected with john". If this is something like facebook, where a person has to allow a connection, then you'll want to build the connections both ways. If the connection is only in one direction, it means that the connection hasn't been approved by the other party yet.

Please review more normalized database designs suggested by other users.

firstly it would indeed be better to structure as SAGExSDX suggests...

to find any relationship:

SELECT * from connections where user1 = X OR user2 = X

Replace X with the username you need - or better the userID if you do refactor AS SAGExSDX suggests

I agree that the code isn't the best, but if you want a query which will grab double links, you'll have to be careful about the direction as mentioned by Marcus. It takes four distinct queries (joined with a union) using the table structure you describe:

SELECT user2 FROM connections
WHERE user1 IN (
    SELECT user2 FROM connections
    WHERE user1 = 'alex'
)
UNION
SELECT user1 FROM connections
WHERE user2 IN (
    SELECT user1 FROM connections
    WHERE user2 = 'alex'
)
UNION 
SELECT user1 FROM connections
WHERE user2 IN (
    SELECT user2 FROM connections
    WHERE user1 = 'alex'
)
AND user1 <> 'alex'
UNION
SELECT user2 FROM connections
WHERE user1 IN (
    SELECT user1 FROM connections
    WHERE user2 = 'alex'
)
AND user2 <> 'alex'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM