Jump to content
php.lv forumi

MS SQL IF ...


NBS

Recommended Posts

Sveiki, vēlējos pajautāt kā pareizi pielieto IF pie WHERE nosacījuma.

 

Mans kods:

SELECT u.microregion, microid AS id, name FROM microregions AS m 
INNER JOIN users AS u ON u.usern = 'vērtība'
WHERE 
IF u.microregion = '%' THEN 1 = 1 ELSE microid IN (u.microregion) END
ORDER BY name

 

Veidojas kļūda:

Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'IF'.
Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'THEN'.

 

Izpalīdziet, lūdzu.

Edited by NBS
Link to comment
Share on other sites

bubu, runa ir par MS SQL :)

 

Tātad ja ļoti liela vēlme var kaut kā rakstīt CASE, bet WHERE klauzā tas ir diezgan nedabiski, ja vispār to var.

Bet šai gadījumā būs

WHERE u.microregion = '%' OR

 

nu un te aiz OR ir problēma jo es nezinu kā vajag:

ja vajag, ka tā paša ieraksta microregions.microid (es pieņemu, ka šis lauks ir pie pareizās tabulas) sakrīt ar users.microregion, tad pēc OR rakstam microid = u.microregion.

Ja vajag, ka microregions.microid sakrīt ar JEBKURU users.microregion vērtību, tad jāraksta IN apakšvaicājums microid IN (SELECT microregion FROM users).

 

Gints Plivna

http://datubazes.wordpress.com

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...