0

I have this Query:

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;

The query works fine, but if we look the value invoice_code is set 5 times. I wonder if there is any way to declare a global variable to assign the same value to all

2
  • 1
    You can of course do that in a stored procedure Commented Jun 25, 2012 at 19:42
  • Do you can let me an example About stored procedure? referring to my question? Commented Jun 25, 2012 at 19:45

1 Answer 1

3

Sure, you can use user defined variables.

For example:

SET @invoice_code=1;

SELECT MIN(qa_returns.returnlog_code) 
FROM qa_returns 
WHERE qa_returns.invoice_code = @invoice_code
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.