Can other table join depending on the value of the field?

Asked 2 years ago, Updated 2 years ago, 25 views

I'm getting more and more table queries.

I'm leaving a question because I'm wondering if it's possible.

It could be a bit absurd (from what I know)

)

For example,

There is a table called A and a table called B It is joined by one field in a and idx in b.

Suppose you have a - b structure now, but you need other information, so you might need to join in the form a - b - c.

The problem is not only the a-b-c relationship, but also the case of joining another table like a-b-d

I wonder if I can make a type of field in b and divide it into 0, 1, 2, etc., and join another table according to the value.

I don't think it makes sense If that's possible, I think the relationship will be very simple because it's different depending on the type.

If possible, please give me a hint!

sql

2022-09-21 22:01

1 Answers

If the intention of the question is to change the JOIN table according to the value of the record in the table, I don't think so.

Here's how to make it similar:

In a -b -c and a -b -d, it seems that c and d should have a common partial key for a-b.

SELECT ... FROM a - b LEFT JOIN c ON ... LEFT JOIN d ON ...

If you look up all types, you should LEFT JOIN all related tables as above. So some records have columns of c, columns of d are NULL, some records are NULL The column of c is NULL, and the column of d is going to be present.

In particular, when inquiring about a specific type, I think you only need to sign the table for that type as follows.

SELECT ... FROM a-b LEFT JOIN <c or d> ON a-b.type=<0,...> and ...


2022-09-21 22:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.