yeahz Posted October 17, 2012 Report Share Posted October 17, 2012 Sveiki. Netiegu galā, kāpēc viņš neņem to, kas ir prasīts pēdējajā kolonnā? Bilde - http://www.bildites....3gvkdp2j6z0.png Vajag tā: ja pirmā bilde nav tukša, tiek ņemta pirmā bilde, pretēji tiek ņemta otra bilde. Query: SELECT id, title, description_small, picture1, picture2, IF (picture1 = NULL, picture2, picture1) FROM ( SELECT p.id, p.title, p.description_small, i.picture1, i2.picture2 FROM products p LEFT JOIN (SELECT picture AS picture1, product_id FROM images WHERE main='yes') AS i ON i.product_id=p.id LEFT JOIN (SELECT picture AS picture2, product_id FROM images ORDER BY id ASC) AS i2 ON i2.product_id=p.id WHERE p.visible='yes') AS a Quote Link to comment Share on other sites More sharing options...
Val Posted October 17, 2012 Report Share Posted October 17, 2012 Jēga iespringt un sarežģīt, ja var nolasīt gan pic#1, gan pic#2 un php galā pārbaudīt, vai pic#1 nav tukšs...? Quote Link to comment Share on other sites More sharing options...
aaxc Posted October 17, 2012 Report Share Posted October 17, 2012 Apmēram šādi tev neder? SELECT id, title, description_small, CASE WHEN picture1 IS NULL THEN picture2 ELSE picture1 END AS picture FROM products Quote Link to comment Share on other sites More sharing options...
yeahz Posted October 17, 2012 Author Report Share Posted October 17, 2012 Val - Nebiju iedomājies, paldies :D aaxc - paldies, tas nostrādāja! Quote Link to comment Share on other sites More sharing options...
vbz Posted October 17, 2012 Report Share Posted October 17, 2012 Domāju(iespējams), ja pieliktu klāt alias: IF (a.picture1 = NULL, a.picture2, a.picture1) tad tas if's nostrādātu arī tajā kvēriņā Quote Link to comment Share on other sites More sharing options...
yeahz Posted October 17, 2012 Author Report Share Posted October 17, 2012 tā nekas neimainās, mēģināju jau pirmstam, bet paldies. Quote Link to comment Share on other sites More sharing options...
xPtv45z Posted October 17, 2012 Report Share Posted October 17, 2012 Ja tu savā variantā ieliec ...IF (picture1 IS NULL, picture2, picture1)... arī nestrādā? Quote Link to comment Share on other sites More sharing options...
e-remit Posted October 17, 2012 Report Share Posted October 17, 2012 Kļūda bija tajā, ka SQLā salīdzināšana ar NULL vienmēr būs FALSE, t.i. (picture1 = NULL) nekad nenostrādās. Salīdzināšanai ar NULL jālieto (picture1 IS NULL). Quote Link to comment Share on other sites More sharing options...
Mr.Key Posted October 17, 2012 Report Share Posted October 17, 2012 (edited) 1. IS NULL (+e-remit) 2. Sk. f-iju IFNULL() (..., IFNULL(picture1, picture2) AS picture ...) 3. Praksē varētu gadīties, ka jāsalīdzina gan IS NULL, gan = '' (tukšs teksts). 4. Jēga iespringt un saprast, kā darbojas, IR. Tas gan neattiecas uz tiem, kuriem patīk rakstīt kodu, nesaprotot, ko tas dara un kā tas viss darbojas (galvenais, ka strādā). Edited October 17, 2012 by Mr.Key Quote Link to comment Share on other sites More sharing options...
Val Posted October 17, 2012 Report Share Posted October 17, 2012 4. Protams, tā vietā, lai uzrakstītu vienkāršu kveriju bez subselektiem un joiniem... iemesls sarežģīšanai. Way to go :) Par 1.-3. punktiem piekrītu. Quote Link to comment Share on other sites More sharing options...
Kavacky Posted October 18, 2012 Report Share Posted October 18, 2012 SELECT COALESCE(`picture1`, `picture2`) AS `picture` FROM Quote Link to comment Share on other sites More sharing options...
aaxc Posted October 18, 2012 Report Share Posted October 18, 2012 SELECT COALESCE(`picture1`, `picture2`) AS `picture` FROM +1 par šo, nemaz nezināju tādu fīču 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.