Questions related to mysql query.

Asked 2 years ago, Updated 2 years ago, 31 views

DB is using mysql but I'm a beginner at SQL so I don't know how to organize the query.

First of all, there are three tables.
user
uid | int

thing
cid | int
uid | int
content | varchar

mark
mid | int
cid | int
uid | int

I drew it a little bit briefly, but to explain it to you, thing is a post that stores cid and uid, the author's ID. mark is a bookmark format, mid is a unique ID, cid is the ID of a post marked with a bookmark, and uid is the ID of a user marked with a bookmark.

I want to get the contents of the post bookmarked by this user with uid.

Input

uid

Output

[
    {
        cid : ~~~, 
        content: ~~~
    },{
        cid : ~~~, 
        content: ~~~
    },{
        cid : ~~~, 
        content: ~~~
    }
]

mysql sql

2022-09-22 18:29

1 Answers

The problem situation itself is a very typical case, and you can filter the table that joins thing and mark by mark.uid criteria, and then select the columns in thing.

select c.*
from thing as c
  left join mark as m
    on m.cid = c.cid
where m.uid = (wanted UID);

I've created an online SQL demo.


2022-09-22 18:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.