Home > database >  SQL retrieval condition
SQL retrieval condition

Time:10-13

Now have to retrieve conditions A, B, C
Want is data and order (, because as long as 20)

At the same time satisfy A, B, C
+
At the same time satisfy A, B
+
Only meet A

Now I can think of is to use the union

The select... Where A and B and C
The union
The select... Where A and B
The union
The select... Where A

But the feeling is very low, there is a lot of duplicate data, with the union should slow efficiency

What's the idea?
Otherwise, only partial time to retrieve the code

CodePudding user response:

Not as good as the data found out, the program for sorting, more quickly

CodePudding user response:

Do you want this?
The select... Where (A and B and C) or (A and B) or (A)?


In fact, you retrieve A retrieved in the condition of data has been included (A and B and C) and (A and B), unless the condition is not the same as A, (B and C), (D and E and F),

CodePudding user response:

Query conditions, and then according to the three conditions, flag sorting before take 20 article is given

CodePudding user response:

Suggest to collect the data in the process of put in storage, if must be written to implement in a single statement, is poor efficiency will be some bad (data)

CodePudding user response:

references 4 floor selling fruit net reply:
suggest to collect the data in the process of put in storage, if must be written to implement in a single statement, is the efficiency will be less (large amount of data the difference will be a lot of)

But I think that is to write a stored procedure
The select... Where A and B and C

If result1. Count & lt; 20
The select... Where A and B and ! C

If result1. Count + result2. Count & lt; 20
The select... Where A and ! B and ! C

Retrieve 3
This efficiency is good?

CodePudding user response:

Select the from t where A
Minus
Where A AND C AND select the from t! B

CodePudding user response:

refer to dazzle the purple magic feather 5 floor response:
Quote: refer to 4th floor selling fruit net reply:

Suggest to collect the data in the process of put in storage, if must be written to implement in a single statement, is the efficiency will be less (large amount of data the difference will be a lot of)

But I think that is to write a stored procedure
The select... Where A and B and C

If result1. Count & lt; 20
The select... Where A and B and ! C

If result1. Count + result2. Count & lt; 20
The select... Where A and ! B and ! C

Retrieve 3
This efficiency is good?


Logic: no problem, the efficiency is also possible,

- PS: single statement, try this,
Select *
The from t
Where A
The order by case when C end then 10 else 0 + case when the B 1 else 0 end
  • Related