doumu5023 2018-10-06 14:54
浏览 244

根据条件和用户输入MySQL执行Select语句

I am trying to allow a particular SELECT statement to execute based on a particular string sent to the IF condition. For Example, if the user selects support from the dropdown, a variable key is sent to the sql statement, and the appropriate SELECT Statement is executed.

DECLARE @ strg
SET @strg = 'input string'
IF(SELECT * FROM tickets WHERE ticket_section = @strg)
BEGIN
SELECT
usr_users.uname AS `user`,
support_chat_user.message AS user_message,
support_chat_user.created AS user_chat_date,
support_ticket.`subject` AS ticket_subject,
support_ticket.`status` AS ticket_state,
support_ticket.created AS ticket_date,
support_ticket.modified AS updated_date,
tickets.state
FROM
tickets
INNER JOIN support_chat_user ON support_chat_user.tickets_id = tickets.tick_id
INNER JOIN support_ticket ON support_ticket.tickets_id = tickets.tick_id
INNER JOIN usr_users ON support_chat_user.reporter_id = usr_users.uid
WHERE
tickets.tick_id = '9' AND
usr_users.uid = '1'
END

IF EXISTS(SELECT * FROM tickets WHERE ticket_section = @strg)

BEGIN
SELECT
usr_users.uname AS `user`,
support_chat_user.message AS user_message,
support_chat_user.created AS user_chat_date,
support_ticket.`subject` AS ticket_subject,
tickets.state
FROM
tickets
INNER JOIN support_chat_user ON support_chat_user.tickets_id = tickets.tick_id
INNER JOIN support_ticket ON support_ticket.tickets_id = tickets.tick_id
INNER JOIN usr_users ON support_chat_user.reporter_id = usr_users.uid
WHERE
tickets.tick_id = '9' AND
usr_users.uid = '1'

END
  • 写回答

1条回答 默认 最新

  • douzhang5121 2018-10-06 15:11
    关注

    MySQL does not support IF, unless you put the code in a programming block. Your question is a bit hard to follow, because the text mentions one query but the code as two that seem to be identical.

    You can move the conditional logic to the outermost WHERE clause in your query:

    SELECT u.uname AS `user`, scu.message AS user_message, scu.created AS user_chat_date,
           st.`subject` AS ticket_subject, st.`status` AS ticket_state, st.created AS ticket_date, st.modified AS updated_date,
           t.state
    FROM tickets t JOIN
         support_chat_user scu
         ON scu.tickets_id = t.tick_id JOIN
         support_ticket st
         ON st.tickets_id = t.tick_id JOIN
         usr_users u
         ON scu.reporter_id = u.uid
    WHERE t.tick_id = 9 AND  -- presumably this is not numeric
          u.uid = 1 AND      -- presumably this is not numeric
          EXISTS (SELECT 1 FROM tickets t2 WHERE t2.ticket_section = @strg)
    

    I'm not sure this is what you really want. I suspect you want:

    WHERE t.tick_id = 9 AND  -- presumably this is not numeric
          u.uid = 1 AND      -- presumably this is not numeric
          (t.ticket_section = @strg OR @strg IS NULL)
    

    However, that is merely informed speculation on what would be useful in the situation you describe.

    Notes:

    • Table aliases make the query easier to write and to read (as does proper indentation).
    • Numeric constants should not be introduced with single quotes. I am guessing the ids are really numbers. If they are strings, then single quotes are appropriate.
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c