簡體   English   中英

如何在select語句中使用mysql變量/如何在下一個select中使用'as'變量

[英]how to use mysql variable inside select statement / how to use 'as' variable in next select

select 
count(invoice.id) as count_invoice_id, // i want to simply use count_invoice_id instead of using count(discount_offer.id) again inside the if statement below
if(count(invoice.id) > 1 , 'true','false') // i want to used count_invoice_id here
from invoice as invoice
left join discount_offer as discount_offer 
on invoice.id = discount_offer.invoice_id;

上面是我的sql,如果我想在if語句中使用count_invoice_id,是否可能。 目前我所做的是在invoice.id列上再次重復count方法。

我想在上面的sql中的if語句中從'count(invoice.id)'更改為'count_invoice_id'

您可以使用子查詢:

SELECT count_invoice_id, IF(count_invoice_id > 1 , 'true', 'false')
FROM   (SELECT    COUNT(invoice.id) AS count_invoice_id
        FROM      invoice
        LEFT JOIN discount_offer ON invoice.id = discount_offer.invoice_id) t

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM