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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题