|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Unique columns in sort by importance result
I try to search my data and sort the result by importance.
I'm using a MS Access database and my data (table1) looks like this: Code:
ID NAME TEXT 1 Apples Good red apples 2 Bananas Fine yellow bananas 3 Yellow apples Great yellow apples I want to search the data and get a result where the column "NAME" is more important than "TEXT". My SQL looks like this: Code:
SELECT id,name,text,1 AS searchorder FROM table1 WHERE name LIKE '*yellow*' UNION SELECT id,name,text,2 AS searchorder FROM table1 WHERE text LIKE '*yellow*' ORDER BY searchorder The output is this: Code:
ID NAME TEXT SEARCHORDER 3 Yellow apples Great yellow apples 1 2 Bananas Fine yellow bananas 2 3 Yellow apples Great yellow apples 2 So far so good - the order by importance works - but I do not get unique columns because of the searchorder column. Can I fix my SQL so I get unique columns where the last line of "Yellow apples" does not appear or am I lost in space? Best regards, Peter from Denmark |
|
#2
|
|||
|
|||
|
Code:
SELECT id,[name],[text] FROM table1 WHERE [name] LIKE '*yellow*' or [text] like '*yellow*' order by iif([name] LIKE '*yellow*',1,2) |
|
#3
|
|||
|
|||
|
Great! Thank you! It works fine!
In fact I have 4 different colums I want to sort by importance, so I have extended your SQL to this: Code:
SELECT id,[name],[text],[desc],[art] FROM table1 WHERE [name] LIKE '*yellow*' OR [text] LIKE '*yellow*' OR [desc] LIKE '*yellow*' OR [text] LIKE '*yellow*' ORDER BY IIF([name] LIKE '*yellow*',1,IIF([text] LIKE '*yellow*',2,IIF([desc] LIKE '*yellow*',3,4))) Peter from Denmark |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Unique columns in sort by importance result |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|