Jump to content
php.lv forumi

SELECT IF


yeahz

Recommended Posts

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

Link to comment
Share on other sites

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 by Mr.Key
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...