gpt4 book ai didi

mysql - 全局变量 MySQL

转载 作者:行者123 更新时间:2023-11-29 06:06:48 25 4
gpt4 key购买 nike

我有这个查询:

SELECT 
qa_invoices.invoice_clientname,
(
SELECT IFNULL(MIN(qa_returns.discount_code),1)
FROM qa_returns
WHERE qa_returns.invoice_code = qa_invoices.invoice_code
AND qa_returns.discount_code <> 1
) AS discount_code,
qa_users.user_name,
(0.00) AS previous_balance,
(0.00) AS difference_to_be_paid,
(0.00) AS client_credit,
SUM(SubQueryAlias.item_discount) AS invoice_discount,
SUM(SubQueryAlias.item_subtotal) AS invoice_subtotal,
SUM(SubQueryAlias.item_total) AS invoice_total,
DATE_FORMAT(qa_invoices.invoice_date,'%M %e, %Y @ %h:%i %p') AS returnlog_date
FROM (
SELECT qa_returns_items.item_code,
qa_returns_items.item_subtotal,
qa_returns_items.item_discount,
qa_returns_items.item_total
FROM qa_returns_items
WHERE returnlog_code = (
SELECT MIN(qa_returns.returnlog_code)
FROM qa_returns
WHERE qa_returns.invoice_code = 1
)
UNION
SELECT qa_returns_residues.item_code,
qa_returns_residues.item_subtotal,
qa_returns_residues.item_discount,
qa_returns_residues.item_total
FROM qa_returns_residues
WHERE returnlog_code = (
SELECT MIN(qa_returns.returnlog_code)
FROM qa_returns
WHERE qa_returns.invoice_code = 1
)
ORDER BY item_code ASC
) AS SubQueryAlias, qa_invoices
LEFT JOIN qa_users USING(user_code)
WHERE SubQueryAlias.item_code NOT IN (
SELECT a.item_code
FROM qa_returns_items a
JOIN qa_returns_residues b
ON b.item_code = a.item_code
WHERE a.returnlog_code = (
SELECT MIN(qa_returns.returnlog_code)
FROM qa_returns
WHERE qa_returns.invoice_code = 1
)
AND b.returnlog_code = (
SELECT MIN(qa_returns.returnlog_code)
FROM qa_returns
WHERE qa_returns.invoice_code = 1
)
)
AND qa_invoices.invoice_code = 1;

查询工作正常,但如果我们查看值 invoice_code 设置了 5 次。我想知道是否有什么方法可以声明一个全局变量来为所有变量分配相同的值

最佳答案

当然,您可以使用user defined variables .

例如:

SET @invoice_code=1;

SELECT MIN(qa_returns.returnlog_code)
FROM qa_returns
WHERE qa_returns.invoice_code = @invoice_code

关于mysql - 全局变量 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11196217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com