dongming0505 2013-06-12 21:14
浏览 104
已采纳

有没有办法在SQL查询中执行条件逻辑?

So I have a fairly long complex query but the gist is that the query's basic functionality looks like this SELECT verification_id FROM verification_table

The verification_id returns an integer from 0-3. Is there a way to do something in the SQL query where if the verification_id is 0, it returns a string like "new" and different ones for all 4 verification_id's.

I can do this in the backend via PHP but i was wondering if there was a way to do it via MySQL

  • 写回答

2条回答 默认 最新

  • dsfds2343 2013-06-12 21:22
    关注
    SELECT CASE 
             WHEN verification_id = 0 THEN 'new' 
             WHEN verification_id = 1 THEN 'something else' 
             ELSE 'error?' 
           END AS myvalue 
    FROM   verification_table 
    

    something like this should work.

    Altough you may consider using the mysql ENUM field type which basically translates text strings into numbers and vice versa. It is an efficient field type of storing such values.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程