Is there any way to get the if statement to evaluate a query? SELECT if(5>0,'EQ_Type','*') FROM EQUIPMENT; Resulting in: +-----------------------+ | IF(5>0,'EQ_Type','*') | +-----------------------+ | EQ_Type | | EQ_Type | | EQ_Type | | EQ_Type | | EQ_Type | | EQ_Type | | EQ_Type | | EQ_Type | | EQ_Type | +-----------------------+ 9 rows in set (0.00 sec) I would like the above statement to be equivalent to the following: SELECT 'EQ_Type' FROM EQUIPMENT; And produce: +--------------+ | EQ_Type | +--------------+ | ENGINE | | ENGINE | | ENGINE | | TRAILER | | TRAILER | | TRAILER | | WATER TENDER | | WATER TENDER | | WATER TENDER | +--------------+ Thanks for any help
1
A
Répondre
3
Supprimez simplement les guillemets simples.
SELECT if(5>0,EQ_Type,'*') FROM EQUIPMENT;
+0
Merci monsieur. Je suis un novice alors j'ai pensé que ce serait quelque chose d'évident. –
1
Je recommande d'utiliser la notation ANSI - CASE statements dans ce cas - plutôt que MySQL syntaxe spécifique:
SELECT CASE WHEN 5 > 0 THEN t.eq_type ELSE '*' END
FROM EQUIPMENT t
Raison d'être, si vous avez modifié les bases de données - cette déclaration ne serait pas nécessaire de modifier travailler sur Oracle, Postgres, SQL Server, etc.
Questions connexes
- 1. javascript if instruction question
- 2. Erreur MySQL à partir d'une instruction IF
- 3. MYSQL Stored Procedures If Instruction Problème
- 4. MySQL: group by et instruction IF
- 5. comparaisons question objective-c de base re dans instruction if
- 6. courte main instruction if
- 7. Ant if instruction
- 8. leave if instruction
- 9. Jquery - opérateurs instruction if
- 10. Jquery if instruction help
- 11. perl instruction if
- 12. avec une instruction if
- 13. instruction if en python
- 14. avec instruction if
- 15. Instruction JQuery if
- 16. jQuery if instruction, syntaxe
- 17. Raccourcir cette instruction IF
- 18. Javascript/JQuery instruction if
- 19. Sql query - if instruction
- 20. cas sql/instruction if
- 21. Instruction if/else Javascript if/else
- 22. Fonction OOP et instruction if
- 23. Aide création sélective instruction IF
- 24. Comment mettre une instruction IF dans une requête MySQL
- 25. MySQL - La sélection basée sur une instruction if
- 26. Comment avoir une plage dans une instruction MySQL If?
- 27. C# if instruction avec dropdownlist.selectedvalue
- 28. jquery if instruction animate back
- 29. instruction if intérieur tableau: CodeIgniter
- 30. if else instruction dans formview
Wont 5> 0 aboutit toujours à 'true', d'où EQ_Type? Je ne connais pas très bien MySql donc je pourrais mal lire ça. Était-ce juste un exemple artificiel? – GrayWizardx