2017-04-04 73 views
0

我試圖表現出「YES」如果值中包含的信息:顯示自定義的數據,如果該值包含在SQL查詢信息

這裏我再使與id_order的關係:

LEFT JOIN ps_surchage_eq_orders re ON a.`id_order`=re.`id_order` 

如果當前「id_order」有這個關係的任何數據,可以顯示值「YES」?

SELECT SQL_CALC_FOUND_ROWS DISTINCT a.`id_order`, a.`invoice_number` AS num_factura,a.`delivery_number` AS num_albaran,a.`reference`, CONCAT(c.`firstname`, ' ', c.`lastname`) AS `customer`, c.email, addr.dni, gr.name AS group_name, a.total_discounts_tax_excl,a.total_paid_real, re.`id_order` AS RE, a.`payment`, a.`date_add` AS `date_add` , a.id_currency, a.id_order AS id_pdf, osl.`name` AS `osname`, IF((SELECT so.id_order 

FROM `ps_orders` so 

WHERE so.id_customer = a.id_customer AND so.id_order < a.id_order LIMIT 1) > 0, 0, 1) as new, country_lang.name as cname, IF(a.valid, 1, 0) badge_success 

FROM `ps_orders` a 

LEFT JOIN `ps_customer` c ON (c.`id_customer` = a.`id_customer`) 

LEFT JOIN `ps_address` address ON address.id_address = a.id_address_delivery 

LEFT JOIN `ps_country` country ON address.id_country = country.id_country 

LEFT JOIN `ps_country_lang` country_lang ON (country.`id_country` = country_lang.`id_country` AND country_lang.`id_lang` = 1) 

LEFT JOIN `ps_order_state` os ON (os.`id_order_state` = a.`current_state`) 

LEFT JOIN `ps_order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = 1) 

LEFT JOIN `ps_address` addr ON (c.id_customer=addr.id_customer) 

LEFT JOIN ps_group_lang gr ON c.id_default_group=gr.id_group AND gr.id_lang=a.id_lang 

LEFT JOIN ps_surchage_eq_orders re ON a.`id_order`=re.`id_order` 

WHERE 1 

ORDER BY a.`id_order` DESC 

謝謝!

+0

添加一些示例數據和預期結果 – 2017-04-04 09:32:15

+0

idorder name order re(行示例),在行中如果在「ps_surchage_eq_orders」表中有一個id順序,則顯示「YES」,最後一個表是訂單號 –

回答

0

更換re.id_order AS REIF(re.id_order IS NULL, 'YES', 'NO') AS RE

SELECT SQL_CALC_FOUND_ROWS DISTINCT a.`id_order`, a.`invoice_number` AS num_factura,a.`delivery_number` AS num_albaran,a.`reference`, CONCAT(c.`firstname`, ' ', c.`lastname`) AS `customer`, c.email, addr.dni, gr.name AS group_name, a.total_discounts_tax_excl,a.total_paid_real, IF(re.`id_order` IS NULL, 'YES', 'NO'), a.`payment`, a.`date_add` AS `date_add` , a.id_currency, a.id_order AS id_pdf, osl.`name` AS `osname`, IF((SELECT so.id_order 

FROM `ps_orders` so 

WHERE so.id_customer = a.id_customer AND so.id_order < a.id_order LIMIT 1) > 0, 0, 1) as new, country_lang.name as cname, IF(a.valid, 1, 0) badge_success 

FROM `ps_orders` a 

LEFT JOIN `ps_customer` c ON (c.`id_customer` = a.`id_customer`) 

LEFT JOIN `ps_address` address ON address.id_address = a.id_address_delivery 

LEFT JOIN `ps_country` country ON address.id_country = country.id_country 

LEFT JOIN `ps_country_lang` country_lang ON (country.`id_country` = country_lang.`id_country` AND country_lang.`id_lang` = 1) 

LEFT JOIN `ps_order_state` os ON (os.`id_order_state` = a.`current_state`) 

LEFT JOIN `ps_order_state_lang` osl ON (os.`id_order_state` = osl.`id_order_state` AND osl.`id_lang` = 1) 

LEFT JOIN `ps_address` addr ON (c.id_customer=addr.id_customer) 

LEFT JOIN ps_group_lang gr ON c.id_default_group=gr.id_group AND gr.id_lang=a.id_lang 

LEFT JOIN ps_surchage_eq_orders re ON a.`id_order`=re.`id_order` 

WHERE 1 

ORDER BY a.`id_order` DESC 

如果ps_surchage_eq_orders不具有id_order這將是空的,如果在將返回YES,如果有也不會爲空,將返回NO

+0

,謝謝! !但是我改變了「是」和「否」的順序,因爲當值爲空時顯示「是」 –

+0

很高興提供幫助。如果您希望在空或不空時顯示是,我理解錯誤。 – sadlyblue

相關問題