NBS Posted March 2, 2009 Report Share Posted March 2, 2009 (edited) 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 March 2, 2009 by NBS Quote Link to comment Share on other sites More sharing options...
bubu Posted March 2, 2009 Report Share Posted March 2, 2009 MySQL'ā IF'us pieraksta ar sintaksi: IF(nosacījumus, ja_true, ja_false) http://dev.mysql.com/doc/refman/5.0/en/con...tml#function_if Quote Link to comment Share on other sites More sharing options...
Gints Plivna Posted March 2, 2009 Report Share Posted March 2, 2009 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 Quote Link to comment Share on other sites More sharing options...
bubu Posted March 3, 2009 Report Share Posted March 3, 2009 ups, nepamanīju topika nosaukumu. Skatījos tekstā un neredzēju DB nosaukumu - tātad pieņemam defaulto MySQL :) Quote Link to comment Share on other sites More sharing options...
NBS Posted March 3, 2009 Author Report Share Posted March 3, 2009 Uh, nu jā, pats neiedomājos, ka šadi vajag: "tad jāraksta IN apakšvaicājums microid IN (SELECT microregion FROM users)." Paldies ;) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.