Home > database >  For SQL left join one-to-many matching problem
For SQL left join one-to-many matching problem

Time:12-10

Parts list
Parts level
A
B
C
D


The users table
User area
The northeast
Northwest
North China
Central China
East China
The southeast
Southwest

When parts grade A can match the northeast, northwest
When the grade B parts can match the northeast, northwest, north China, central China, southwest
When the parts level C can match southeast
When grade D parts can match central China, southwest


How to achieve this?

CodePudding user response:

I need a relational table
 
The CREATE TABLE # A (component level VARCHAR (20))
# INSERT INTO A values (' A ')
# INSERT INTO A values (' B ')
# INSERT INTO A values (' C ')
# INSERT INTO A values (' D ')
The CREATE TABLE # B (user area VARCHAR (20))
INSERT INTO # B values (' northeast)
INSERT INTO # B values (' northwest)
INSERT INTO # B values (' north ')
INSERT INTO # B values (' central China ')
INSERT INTO # B values (' east ')
INSERT INTO # B values (' southeast)
INSERT INTO # B values (' southwest)
The Create table # C (component level VARCHAR (20), the user area VARCHAR (50))
INSERT INTO # C values (' A ', 'northeast, northwest)
INSERT INTO # C values (' B ', 'the northeast, northwest, north China, central China, southwest')
INSERT INTO # C values (' C ', 'the southeast)
INSERT INTO # C values (' D ', 'central China, southwest)

Select * from # A, A, B # B
The where (select COUNT (1) the from C # C where c. parts level=a. parts level and c. user area like '%' + b. user area + '%') & gt; 0

CodePudding user response:

I only have permission to query the database, what function can realize the function?

CodePudding user response:

Build a relational table should be built like upstairs to answer directly, rather like he code to build a relational table

CodePudding user response:

refer to the second floor xiaoks668 response:
I only have permission to query the database, what is the function can implement this feature?

Today, you would have to build the relationship table
 
The CREATE TABLE # A (component level VARCHAR (20))
# INSERT INTO A values (' A ')
# INSERT INTO A values (' B ')
# INSERT INTO A values (' C ')
# INSERT INTO A values (' D ')
The CREATE TABLE # B (user area VARCHAR (20))
INSERT INTO # B values (' northeast)
INSERT INTO # B values (' northwest)
INSERT INTO # B values (' north ')
INSERT INTO # B values (' central China ')
INSERT INTO # B values (' east ')
INSERT INTO # B values (' southeast)
INSERT INTO # B values (' southwest)

Select * from # A, A, B # B
The where (select COUNT (1) the from
(select the 'A' level of parts, users' northeast, northwest 'regional union all
Select 'B' component level, northeast, northwest, north China, central China, southwest 'union all user area
Level, select the 'C' parts' southeast 'union all user area
Level, select the 'D' parts' central China, southwest user area) c
Where c. parts level=a. parts level and c. user area like '%' + b. user area + '%') & gt; 0


CodePudding user response:

Only the permissions query with a temporary table or table variable is solved
  • Related