Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL] CASE albo IF w WHERE
Elber
post
Post #1





Grupa: Zarejestrowani
Postów: 381
Pomógł: 12
Dołączył: 9.07.2007

Ostrzeżenie: (0%)
-----


Witam,

mam problem z zapytaniem z CASE bądź IF w WHERE (IMG:style_emoticons/default/smile.gif)

Mam zapytanie:
  1. SELECT
  2. c.*, s.name AS sname,
  3. p.name AS pname
  4. FROM
  5. certificate c LEFT JOIN
  6. products p ON c.products_id = p.products_id LEFT JOIN
  7. STATUS s ON c.status_id = s.status_id
  8. WHERE
  9.  
  10. p.name LIKE "%"
  11. AND
  12. c.filename LIKE "%"
  13. AND
  14. c.status_id LIKE "%"
  15.  
  16. ORDER BY
  17. c.certificate_id
  18. DESC


Które działa jeśli c.products_id > 0 a gdy jest = 0 to tego rekordu nie wyświetla co jest logiczne ( bo szukam tak że po nazwie produtku (p.name)).

Pytanie jak to obejść ? Żeby rekordy z c.products_id również wyświetlał ?

Próbuję:

  1. SELECT
  2. c.*, s.name AS sname,
  3. p.name AS pname
  4. FROM
  5. certificate c LEFT JOIN
  6. products p ON c.products_id = p.products_id LEFT JOIN
  7. STATUS s ON c.status_id = s.status_id
  8. WHERE
  9.  
  10. CASE WHEN c.products_id > 0 THEN p.name LIKE "%" ELSE NULL END
  11. AND
  12. c.filename LIKE "%"
  13. AND
  14. c.status_id LIKE "%"
  15.  
  16. ORDER BY
  17. c.certificate_id
  18. DESC

Jednak wyświetla tylko te z c.products_id > 0 (IMG:style_emoticons/default/sad.gif)

Ten post edytował Elber 26.02.2013, 23:56:58
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
CuteOne
post
Post #2





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

Ostrzeżenie: (0%)
-----


  1. IF (c.product_id > 0, p.name LIKE '%%')
Go to the top of the page
+Quote Post
Elber
post
Post #3





Grupa: Zarejestrowani
Postów: 381
Pomógł: 12
Dołączył: 9.07.2007

Ostrzeżenie: (0%)
-----


  1. SELECT
  2. c.*, s.name AS sname,
  3. p.name AS pname
  4. FROM
  5. certificate c LEFT JOIN
  6. products p ON c.products_id = p.products_id LEFT JOIN
  7. STATUS s ON c.status_id = s.status_id
  8. WHERE
  9. IF(c.products_id > 0, p.name LIKE "%")
  10. AND
  11. c.filename LIKE "%"
  12. AND
  13. c.status_id LIKE "%"
  14.  
  15. ORDER BY
  16. c.certificate_id
  17. DESC


#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') AND c.filename LIKE "%" AND c.status_id LIKE "%" ORDER BY c.certificate_' at line 9
Go to the top of the page
+Quote Post
CuteOne
post
Post #4





Grupa: Zarejestrowani
Postów: 2 958
Pomógł: 574
Dołączył: 23.09.2008
Skąd: wiesz, że tu jestem?

Ostrzeżenie: (0%)
-----


IF(c.products_id > 0, p.name LIKE "%",1)
Go to the top of the page
+Quote Post
Elber
post
Post #5





Grupa: Zarejestrowani
Postów: 381
Pomógł: 12
Dołączył: 9.07.2007

Ostrzeżenie: (0%)
-----


Działa (IMG:style_emoticons/default/smile.gif) Kombinowałem już wczoraj z IF ale zamiast 1 końcu dawałem NULL albo 0 (IMG:style_emoticons/default/biggrin.gif) jakoś mnie tak przyćmiło, a teraz jak napisałem z 1 to mnie oświeciło (IMG:style_emoticons/default/smile.gif) Dzięki zapomoc (IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 18:55