If conditions are given to the JOIN table, the conditions are given accordingly. Regardless of the conditions, if you want to select anything that matches PK

Asked 2 years ago, Updated 2 years ago, 38 views

For example, table subjects that have a users table and a user's PK.

SELECT
    u.id,
    GROUP_CONCAT(DISTINCT sbj.number)

FROM
    users u

JOIN
   subjects sbj
ON
    sbj.userId = u.id AND
    sbj.number IN (1)

WHERE
    u.isDeleted != 1

GROUP BY
    u.id

This is the situation.

When importing a user's list, regardless of JOIN, I always want to bring and show all the subjects.number that the user has, but if you use a grammar like JOIN through filter search, you don't get all the subjects.number that you have, but only get the ones that fit the IN.

It's a natural result, but I want to give you the conditions and bring all the SELECTs regardless of the JOIN statement.

user: {
    subjects: ['Math', 'English', 'Science', 'Social', 'Science']
}

// Even if the math is 1 and the condition is subject.number IN (1), I hope all the subjects that the user has come out!

Please ignore that the result value is STRING. I just explained it to you.

I found two ways. I don't think it's right, so I'm asking you a question.

But I don't think both of them are right, so I'm asking you a question.

mysql

2022-09-22 15:24

1 Answers

It is not known exactly what the requirement is, but once the subject.numberIN(1) condition is included, the whole cannot be imported.

It is possible to use subqueries, but if you create queries in this way, there is a high possibility that you will have problems maintaining them later.

If you don't have to receive it as a request once, I think it would be better to request it separately even if the performance is poor.


2022-09-22 15:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.