V
V
vetsmen2017-08-29 12:04:32
MySQL
vetsmen, 2017-08-29 12:04:32

SQL query with if/else?

There are 2 tables:
1) Quests {id - identifier; title, prize - specific information}
2) QuestsActive {questid - top table identifier, userid - user identifier}
You need to create the following SQL query:
If intersections of the Quests and QuestsActive tables are found (that is, if Quests.id = QuestsActive.questid with a given QuestsActive. userid), then create a column and add 1 there, if no intersections are found, then create a column and add 0 there;
On the output, I have to get all records from the Quests table with title, prize and a new column with values ​​0/1
How can I do this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
V Sh., 2017-08-29
@vetsmen

I didn’t quite understand about “creating a column”, but here is a query that will display the necessary information:

SELECT Quests.*, CASE WHEN ActiveQuests.Id > 0 THEN 1 ELSE 0) AS isActive
FROM Quests
LEFT JOIN ActiveQuests ON Quests.Id = ActiveQuests.Id AND ActiveQuests.UserLogin = 'user'
ORDER BY Quests.Id

D
d-stream, 2017-08-29
@d-stream

case

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question