Different results from Identical databases - mysql

I'm having an issue where my live database (MariaDB) has the exact same data as my local(MySQL) but the following query is returning the same results but in a different order (I know im not the best at SQL so I'll apologise in advance):
SELECT
`products`.*
, CONCAT( `booking_inventory`.`year`, '-', LPAD( `booking_inventory`.`month`, 2, '00' ), '-', LPAD( `booking_inventory`.`day`, 2, '00' ) ) AS `order_date`
, `category_product`.`category_id` AS `pivot_category_id`
, `category_product`.`product_id` AS `pivot_product_id`
, `count_activate`.`active_count`
, IF( `count_activate`.`product_id` > 0, 0, 1 ) AS coming_soon
FROM
`products`
INNER JOIN
`category_product`
ON
`products`.`id` = `category_product`.`product_id`
LEFT JOIN
(
SELECT
inventory.*
FROM
inventory
INNER JOIN
`booking_inventory`
ON
`inventory`.`id` = `booking_inventory`.`inventory_id`
WHERE
CONCAT( `booking_inventory`.`year`, '-', LPAD( `booking_inventory`.`month`, 2, '00' ), '-', LPAD( `booking_inventory`.`day`, 2, '00' ) ) > NOW()
ORDER BY
DATE( CONCAT( `booking_inventory`.`year`, '-', LPAD( `booking_inventory`.`month`, 2, '00' ), '-', LPAD( `booking_inventory`.`day`, 2, '00' ) ) )
)
AS
inventory
ON
`products`.`id` = `inventory`.`product_id`
INNER JOIN
`booking_inventory`
ON
`inventory`.`id` = `booking_inventory`.`inventory_id`
LEFT JOIN
(
SELECT
COUNT(inventory.id) AS active_count
, product_id
FROM
inventory
INNER JOIN
`booking_inventory`
ON
`inventory`.`id` = `booking_inventory`.`inventory_id`
WHERE
status_id = 1
AND
(
stock > 0
OR stock = -1
)
AND
CONCAT( `booking_inventory`.`year`, '-', LPAD( `booking_inventory`.`month`, 2, '00' ), '-', LPAD( `booking_inventory`.`day`, 2, '00' ) ) > NOW()
GROUP BY
product_id
)
AS
count_activate
ON
`count_activate`.`product_id` = `products`.`id`
WHERE
`category_product`.`category_id` = 2
AND EXISTS
(
SELECT
*
FROM
`sites`
INNER JOIN
`product_site`
ON
`sites`.`id` = `product_site`.`site_id`
WHERE
`product_site`.`product_id` = `products`.`id`
AND
`status_id` = 1
AND
`site_id` = 1
)
AND
`products`.`status_id` = 1
AND
CONCAT( `booking_inventory`.`year`, '-', LPAD( `booking_inventory`.`month`, 2, '00' ), '-', LPAD( `booking_inventory`.`day`, 2, '00' ) ) > NOW()
GROUP BY
`products`.`id`
ORDER BY
`coming_soon` ASC
, `order_date` ASC
LIMIT 100
OFFSET 0
can anyone tell me whats causing this?
regards
(left id External right is Local)
EDIT
Thanks for the daft comments below and the down vote, very helpful.... After some digging, I have found the cause yet not the answer. In the second JOIN (inventory) the date ordering isn't returning the same results. If I order by the inventory id, price, SKU I get the same results across local and external data but not using the date... would anyone know why?
regards

The problem was down to the version/dbengine. The first left join has an order by in it, which I didn't realise didn't keep its order once used by the parent (depending on version/dbengine).
One way to overcome this is by setting a limit of 18446744073709551615 which forces the results to be stored in a temp table (or something, i know not what I speak!).
the other issue was inner join further down the query which forced to table to be reordered.
SELECT
IF( `counter`.`product_id` > 0, 0, 1 ) AS coming_soon,
bd.skill_level,
counter.active_count,
p.*
FROM
(
SELECT
p.*,
booking_inventory.inventory_id,
category_product.category_id,
CONCAT( `booking_inventory`.`year`, '-', LPAD( `booking_inventory`.`month`, 2, '00' ), '-', LPAD( `booking_inventory`.`day`, 2, '00' ) ) as date
FROM
booking_inventory
JOIN
inventory
ON
inventory.id = booking_inventory.inventory_id
LEFT JOIN
products AS p
ON
p.id = inventory.product_id
INNER JOIN
`category_product`
ON
`p`.`id` = `category_product`.`product_id`
WHERE
CONCAT( `booking_inventory`.`year`, '-', LPAD( `booking_inventory`.`month`, 2, '00' ), '-', LPAD( `booking_inventory`.`day`, 2, '00' ) ) > NOW()
AND
`category_product`.`category_id` = 2
ORDER BY
date
LIMIT
18446744073709551615
)
AS
p
LEFT JOIN
booking_data AS bd
ON
p.id = bd.product_id
LEFT JOIN
(
SELECT
COUNT(`inventory`.`id`) AS `active_count`,
`inventory`.`product_id`
FROM
`inventory`
INNER JOIN
`booking_inventory`
ON
`inventory`.`id` = `booking_inventory`.`inventory_id`
WHERE
`inventory`.`status_id` = 1
AND
(
`inventory`.`stock` > 0
OR
`inventory`.`stock` = -1
)
AND
CONCAT( `booking_inventory`.`year`, '-', LPAD( `booking_inventory`.`month`, 2, '00' ), '-', LPAD( `booking_inventory`.`day`, 2, '00' ) ) > NOW()
GROUP BY
`inventory`.`product_id`
)
AS
counter
ON
`counter`.`product_id` = `p`.`id`
WHERE
EXISTS
(
SELECT
*
FROM
`sites`
INNER JOIN
`product_site`
ON
`sites`.`id` = `product_site`.`site_id`
WHERE
`product_site`.`product_id` = `p`.`id`
AND
`status_id` = 1
AND
`site_id` = 1
)
GROUP BY
p.id
ORDER BY
coming_soon,
p.date,
p.name

Related

Order Clause on query took too much load time MySQL

I am stuck at a point where I have millions of records and required different joins for the same. Also there are some tricky part for the order clause. My query performs fast result if I'll not apply any ordering. But while applying order clause it takes too much time to get result.
Without Order clause it results in 5-6 seconds.
While applying Order Clause it results in 40-45 second
SELECT
forms_values.id,
CASE
WHEN forms_values.appointment_type = 2
AND user_patient_assinged_to_doctor.start_time IS NOT NULL
THEN
CASE
WHEN patient_responded_tags_logs.tag_set_at IS NOT NULL
THEN
CASE
WHEN UNIX_TIMESTAMP(
CONVERT_TZ(
patient_responded_tags_logs.tag_set_at,
"+00:00",
"-06:00"
)
) > UNIX_TIMESTAMP(
CONVERT_TZ(
STR_TO_DATE(
CONCAT(
user_patient_assinged_to_doctor.date,
" ",
user_patient_assinged_to_doctor.start_time
),
"%Y-%m-%d %h:%i %p"
),
"+00:00",
"-06:00"
)
)
THEN UNIX_TIMESTAMP(
CONVERT_TZ(
patient_responded_tags_logs.tag_set_at,
"+00:00",
"-06:00"
)
)
ELSE UNIX_TIMESTAMP(
CONVERT_TZ(
STR_TO_DATE(
CONCAT(
user_patient_assinged_to_doctor.date,
" ",
user_patient_assinged_to_doctor.start_time
),
"%Y-%m-%d %h:%i %p"
),
"+00:00",
"-06:00"
)
)
END
ELSE UNIX_TIMESTAMP(
CONVERT_TZ(
STR_TO_DATE(
CONCAT(
user_patient_assinged_to_doctor.date,
" ",
user_patient_assinged_to_doctor.start_time
),
"%Y-%m-%d %h:%i %p"
),
"+00:00",
"-06:00"
)
)
END
ELSE
CASE
WHEN patient_responded_tags_logs.tag_set_at IS NOT NULL
THEN
CASE
WHEN UNIX_TIMESTAMP(
CONVERT_TZ(
patient_responded_tags_logs.tag_set_at,
"+00:00",
"-06:00"
)
) > UNIX_TIMESTAMP(forms_values.created_at)
THEN UNIX_TIMESTAMP(
CONVERT_TZ(
patient_responded_tags_logs.tag_set_at,
"+00:00",
"-06:00"
)
)
ELSE UNIX_TIMESTAMP(forms_values.created_at)
END
ELSE UNIX_TIMESTAMP(forms_values.created_at)
END
END AS "consultation_date_time_ordering",
CASE
WHEN forms_values.appointment_type = 2
AND user_patient_assinged_to_doctor.start_time IS NOT NULL
THEN UNIX_TIMESTAMP(
CONVERT_TZ(
STR_TO_DATE(
CONCAT(
user_patient_assinged_to_doctor.date,
" ",
user_patient_assinged_to_doctor.start_time
),
"%Y-%m-%d %h:%i %p"
),
"+00:00",
"-06:00"
)
)
ELSE UNIX_TIMESTAMP(forms_values.created_at)
END AS "consultation_date_time" ,
CASE
WHEN forms_values.is_postpone = '1'
OR forms_values.is_completed = '8'
THEN
CASE
WHEN UNIX_TIMESTAMP(
CONVERT_TZ(
STR_TO_DATE(
CONCAT(UTC_DATE(), ' ', UTC_TIME()),
'%Y-%m-%d %h:%i:%s'
),
'+00:00',
'-06:00'
)
) < UNIX_TIMESTAMP(
my_list_postpone.postponed_date
)
THEN 0
ELSE 1
END
ELSE 1
END AS "postponed_consultation_ordering"
FROM
`forms_values`
LEFT JOIN `forms_values_completed_status_details`
ON `forms_values_completed_status_details`.`form_value_id` = `forms_values`.`id`
/*INNER JOIN `users`
ON `users`.`id` = `forms_values`.`patient_id`
LEFT JOIN `users` AS `doctors`
ON `doctors`.`id` = `forms_values`.`doctor_id`*/
LEFT JOIN `user_patient_assinged_to_doctor`
ON `user_patient_assinged_to_doctor`.`form_value_id` = `forms_values`.`id`
INNER JOIN `states_countries`
ON `forms_values`.`state` = `states_countries`.`id`
LEFT JOIN `user_payment_history`
ON `user_payment_history`.`form_value_id` = `forms_values`.`id`
LEFT JOIN `emailed_tags_logs`
ON `emailed_tags_logs`.`form_value_id` = `forms_values`.`id`
AND `emailed_tags_logs`.`id` =
(SELECT
emailed_tags_logs.id
FROM
emailed_tags_logs
WHERE emailed_tags_logs.form_value_id = forms_values.id
AND emailed_tags_logs.id =
(SELECT
emailed_tags_logs1.id AS emtid
FROM
emailed_tags_logs AS emailed_tags_logs1
WHERE emailed_tags_logs1.form_value_id = forms_values.id
ORDER BY emailed_tags_logs1.created_at DESC
LIMIT 1)
AND emailed_tags_logs.status IN (1, 3)
AND emailed_tags_logs.is_pt_responded = "0"
ORDER BY emailed_tags_logs.created_at DESC
LIMIT 1)
LEFT JOIN `my_list_assign_doctor`
ON `my_list_assign_doctor`.`form_value_id` = `forms_values`.`id`
AND `my_list_assign_doctor`.`id` =
(SELECT
my_list_assign_doctor.id
FROM
my_list_assign_doctor
WHERE my_list_assign_doctor.form_value_id = forms_values.id
AND my_list_assign_doctor.status IN (1, 2)
AND my_list_assign_doctor.prior_type = "others"
ORDER BY my_list_assign_doctor.created_at DESC
LIMIT 1)
LEFT JOIN `my_list_assign_doctor` AS `my_list_postpone`
ON `my_list_postpone`.`form_value_id` = `forms_values`.`id`
AND `forms_values`.`is_postpone` IN ('1', '2')
AND `my_list_postpone`.`id` =
(SELECT
my_list_assign_doctor.id
FROM
my_list_assign_doctor
WHERE my_list_assign_doctor.form_value_id = forms_values.id
AND my_list_assign_doctor.prior_type = "postpone"
ORDER BY my_list_assign_doctor.created_at DESC
LIMIT 1)
LEFT JOIN `users` AS `partner`
ON `user_payment_history`.`std_partner_id` = `partner`.`id`
LEFT JOIN `patient_responded_tags_logs`
ON `patient_responded_tags_logs`.`form_value_id` = `forms_values`.`id`
AND `patient_responded_tags_logs`.`status` = '1'
LEFT JOIN `user_subscriptions`
ON `user_subscriptions`.`user_payment_history_id` = `user_payment_history`.`id`
AND `user_payment_history`.`form_value_id` = `forms_values`.`id`
ORDER BY
postponed_consultation_ordering DESC,
`consultation_date_time` DESC
LIMIT 10 OFFSET 0
Note: All joins are important, and fields are removed from select query for some concerns.
Explain summary
The problem is the combination of LIMIT and ORDER BY.
Without the ORDER BY the query will stop as soon as the first ten random rows are selected. With the order by the query must gather all possible rows then sort them in order then only return the first 10.
There is really no way to make this any faster given that 'postponed_consultation_datetime' is a calculated field.
Some of these composite indexes may be helpful.
user_patient_assinged_to_doctor: INDEX(form_value_id, start_time, date)
patient_responded_tags_logs: INDEX(form_value_id, tag_set_at, status)
my_list_postpone: INDEX(form_value_id, postponed_date, id)
forms_values_completed_status_details: INDEX(form_value_id)
user_payment_history: INDEX(form_value_id, std_partner_id, id)
user_subscriptions: INDEX(user_payment_history_id)
emailed_tags_logs: INDEX(form_value_id, created_at, id)
emailed_tags_logs: INDEX(form_value_id, id, status, is_pt_responded, created_at)
my_list_assign_doctor: INDEX(form_value_id, status, prior_type, created_at, id)
my_list_assign_doctor: INDEX(form_value_id, prior_type, created_at, id)
my_list_assign_doctor: INDEX(form_value_id, id, status, prior_type, created_at)
Don't use LEFT JOIN when you mean INNER JOIN.
FALSE is 0; TRUE is anything else. See if you can simplify some of the CASE statements:
CASE WHEN boolean THEN 1 ELSE 0
is identical to
boolean
Similarly:
CASE WHEN boolean THEN 0 ELSE 1
can probably be replaced by
With proper use of TIMESTAMP versus DATETIME, you can possibly get rid of all the calls to CONVERT_TZ.
NOT boolean
However, this may help the most... Turn the query inside out. See what the minimal effort is to find the LIMIT 10 ids. Hopefully, you only need to look at one, or a small number of, the tables. Then use that as a subquery ("derived table") and then reach for the rest of the columns.

SQL Union all throws SQL Syntax error but the two SELECTS are ok

I want to join 2 SELECT statements. They both work fine individually but if I try to use the Union All I get a syntax error:
[42000][1064] You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near '' at line 29
Which doesn't make any sense to me. I searched for that error message but I don't use quotes in that line (29 is UNION ALL)
SELECT NULL AS id,
table_a.calendar_day AS
calendar_day,
'XXX' AS mbr_entity
,
'total' AS
product,
'total' AS
mbr_stock_category,
'total' AS component,
SUM(IF(table_a.mbr_entity = 'XXX', aim, 0)) AS aim,
SUM(IF(table_a.mbr_entity = 'XXX', cf, 0)) AS cf,
SUM(IF(table_a.mbr_entity = 'XXX', mbr, 0)) AS mbr,
SUM(IF(table_a.mbr_entity = 'XXX', ytd, 0)) AS ytd,
SUM(IF(table_a.mbr_entity = 'XXX', forecast, 0)) AS forecast,
SUM(table_b.a
+ table_b.b
+ table_b.c
+ table_b.d
+ table_b.e) AS actual
FROM table_a
left join table_b
ON table_b.calendar_day =
table_a.calendar_day
AND table_b.mbr_entity =
table_a.mbr_entity
WHERE MONTH(table_a.calendar_day) = MONTH(CURRENT_DATE())
GROUP BY calendar_day
UNION ALL
SELECT NULL AS id,
table_a.calendar_day,
table_a.mbr_entity,
'total' AS product,
'total' AS
mbr_stock_category,
'total' AS component,
SUM(IF(table_a.mbr_entity = 'XXX', 0, aim)) AS aim,
SUM(IF(table_a.mbr_entity = 'XXX', 0, cf)) AS cf,
SUM(IF(table_a.mbr_entity = 'XXX', 0, mbr)) AS mbr,
SUM(IF(table_a.mbr_entity = 'XXX', 0, ytd)) AS ytd,
SUM(IF(table_a.mbr_entity = 'XXX', 0, forecast)) AS forecast,
SUM(table_b.a
+ table_b.b
+ table_b.c
+ table_b.d
+ table_b.e) AS actual
FROM table_a
left join table_b
ON table_b.calendar_day =
table_a.calendar_day
AND table_b.mbr_entity =
table_a.mbr_entity
WHERE table_a.mbr_entity <> 'XXX'
AND MONTH(table_a.calendar_day) = MONTH(CURRENT_DATE())
GROUP BY calendar_day,
mbr_entity
Try Use Code Again
( SELECT NULL AS
id,
table_a.calendar_day AS calendar_day,
'XXX' AS mbr_entity,
'total' AS product,
'total' AS mbr_stock_category,
'total' AS component,
SUM( IF ( table_a.mbr_entity = 'XXX', aim, 0 ) ) AS aim,
SUM( IF ( table_a.mbr_entity = 'XXX', cf, 0 ) ) AS cf,
SUM( IF ( table_a.mbr_entity = 'XXX', mbr, 0 ) ) AS mbr,
SUM( IF ( table_a.mbr_entity = 'XXX', ytd, 0 ) ) AS ytd,
SUM( IF ( table_a.mbr_entity = 'XXX', forecast, 0 ) ) AS forecast,
SUM( table_b.a + table_b.b + table_b.c + table_b.d + table_b.e ) AS actual
FROM
table_a
LEFT JOIN table_b ON table_b.calendar_day = table_a.calendar_day
AND table_b.mbr_entity = table_a.mbr_entity
WHERE
MONTH ( table_a.calendar_day ) = MONTH ( CURRENT_DATE ( ) )
GROUP BY
calendar_day
)
UNION ALL
(
SELECT NULL AS
id,
table_a.calendar_day,
table_a.mbr_entity,
'total' AS product,
'total' AS mbr_stock_category,
'total' AS component,
SUM( IF ( table_a.mbr_entity = 'XXX', 0, aim ) ) AS aim,
SUM( IF ( table_a.mbr_entity = 'XXX', 0, cf ) ) AS cf,
SUM( IF ( table_a.mbr_entity = 'XXX', 0, mbr ) ) AS mbr,
SUM( IF ( table_a.mbr_entity = 'XXX', 0, ytd ) ) AS ytd,
SUM( IF ( table_a.mbr_entity = 'XXX', 0, forecast ) ) AS forecast,
SUM( table_b.a + table_b.b + table_b.c + table_b.d + table_b.e ) AS actual
FROM
table_a
LEFT JOIN table_b ON table_b.calendar_day = table_a.calendar_day
AND table_b.mbr_entity = table_a.mbr_entity
WHERE
table_a.mbr_entity <> 'XXX'
AND MONTH ( table_a.calendar_day ) = MONTH ( CURRENT_DATE ( ) )
GROUP BY
calendar_day,
mbr_entity
)

SQl query showing opposite values

Working on a sql query to show T_Temperatur and T_Badende_per_Time and the values. My sql query changes the comma to dot for the values with Cast, that is not the problem. The problem is that my sql query chose the values of the oppositite title, as you can see here:
SQL:
SELECT routines.date, routines.time,
SUM( IF( measurements.title = 'T_Temperatur', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), 0 ) ) AS T_Temperatur,
SUM( IF( measurements.title = 'T_Badende_per_Time', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), 0 ) ) AS Badende,
SUM( IF( measurements.title = 'T_Luft_temperatur', CAST( REPLACE( routines.value, ',', '.' ) AS DECIMAL( 18, 2 ) ), 0 ) ) AS Luft_Temp
FROM routines
INNER JOIN measure_routine ON routines.id = measure_routine.routine_id
INNER JOIN measurements ON measure_routine.measure_id = measurements.id
GROUP BY routines.date, routines.time
ORDER BY routines.date, routines.time;
My database is build up with
routines:
measure_routine:
measurements:
Swap the parameters in the IF() function, you return 0 if the condition is true!
IF(expr1,expr2,expr3)
If expr1 is TRUE (expr1 <> 0 and expr1 <> NULL) then IF() returns expr2; otherwise it returns expr3.
SELECT routines.date, routines.time,
SUM(IF(measurements.title='T_Temperatur', CAST(REPLACE(routines.value,',','.' ) AS DECIMAL(18,2)), 0)) AS T_Temperatur,
SUM(IF(measurements.title='T_Badende_per_Time', CAST(REPLACE(routines.value,',','.') AS DECIMAL(18,2)), 0)) AS Badende
FROM routines
INNER JOIN measure_routine ON routines.id = measure_routine.routine_id
INNER JOIN measurements ON measure_routine.measure_id = measurements.id
order by routines.date, routines.time

How to sum the percent to be exactly 100.00%?

I have a problem how to sum the percent to be 100.00. i try use the "Round" but the result just show 99.93. Anyone got the idea?.
SELECT Sum(percent)
FROM (SELECT state,
jantina,
bilmale,
jds,
jumall,
Round(( bilmale * 100 / ( jumall ) ), 2) PERCENT,
( jds ) * 100 / ( jumall ) AS p_jdk
FROM (SELECT Timestamp(X.createdate)
dattime
,
Upper(Date_format(X.createdate, '%d %M %Y')) AS
datenegeri,
X.state,
Upper(CASE
WHEN g.ns_fender_desc = 'male' THEN 'LELAKI'
WHEN g.ns_fender_desc = 'female' THEN 'PEREMPUAN'
ELSE g.ns_fender_desc
END) jantina
,
Sum(CASE
WHEN X.male = 1 THEN 1
WHEN X.male = 0 THEN 1
ELSE 0
END) bilmale
,
(SELECT Sum(CASE
WHEN Y.male = 1 THEN 1
WHEN Y.male = 0 THEN 1
ELSE 0
END) bilmale
FROM xyuser_userext Y
JOIN tnsstate TS
ON TS.ns_state_id = Y.state
LEFT JOIN tnsgender g
ON g.ns_gender_id = Y.male
WHERE Y.statususerext IN ( '0', '1', '2', '5' )
AND Y.state = X.state
AND Date_format(X.createdate, '%d %M %Y') =
Date_format(Y.createdate, '%d %M %Y')
GROUP BY TS.ns_state_desc,
Date_format(X.createdate, '%d %M %Y')) jds,
(SELECT Sum(CASE
WHEN Y.male = 1 THEN 1
WHEN Y.male = 0 THEN 1
ELSE 0
END) bilmale
FROM xyuser_userext Y
JOIN tnsstate TS
ON TS.ns_state_id = Y.state
LEFT JOIN tnsgender g
ON g.ns_gender_id = Y.male
WHERE Y.statususerext IN ( '0', '1', '2', '5' ))jumall
FROM xyuser_userext X
JOIN tnsstate TS
ON TS.ns_state_id = X.state
LEFT JOIN tnsgender g
ON g.ns_gender_id = X.male
WHERE X.statususerext IN ( '0', '1', '2', '5' )
GROUP BY TS.ns_state_desc,
Date_format(X.createdate, '%d %M %Y'),
X.male
ORDER BY 1 ASC) AS A) AS b
If you round only the result and not the individual items, it will be more accurate. Try using SELECT Round(Sum(percent), 2) and remove the Round from the percent calculation in the WHERE clause.

How to display result and handle errors # 1242?

I want to:
if the value qty_out! = 0, then set qty_out = 0
Help me to display results
SELECT
SUBSTR(stok_control.tgl_faktur,9,2) AS 'tanggal',
SUBSTR(stok_control.tgl_faktur,6,2) AS 'bulan',
CONCAT(
(SELECT(
IFNULL(stok_control.faktur_beli,''))
)
,
(SELECT(
IFNULL(stok_control.faktur_jual,''))
)
) AS 'faktur',
bahan.id AS 'kode_bahan',
bahan.nm_bahan AS 'nama_bahan',
stok_control.qty_in AS 'masuk',
(
SELECT IF(stok_control.qty_out != '',0,0)
FROM
stok_control
WHERE
stok_control.faktur_beli !=''
) AS 'keluar'
FROM
stok_control
LEFT JOIN bahan ON stok_control.id_bahan=bahan.id
#eggyal, I have changed your code to be:
SELECT
SUBSTR(sc.tgl_faktur, 9, 2) AS 'tanggal',
SUBSTR(sc.tgl_faktur, 6, 2) AS 'bulan',
CONCAT(
IFNULL(sc.faktur_beli, ''),
IFNULL(sc.faktur_jual, '')
) AS 'faktur',
b.id AS 'kode_bahan',
b.nm_bahan AS 'nama_bahan',
(SELECT IFNULL(sc.qty_in,0)) AS 'masuk',
(SELECT
(
IF(faktur_beli <> '',
0,
(SELECT sc.qty_out)
)
)
) AS 'qty_out'
FROM
stok_control sc LEFT JOIN bahan b ON sc.id_bahan = b.id
I've tried to reference an existing but still error...
I want to:
if the value qty_out! = 0, then set qty_out = 0
Do you mean that you want qty_out to be 0 irrespective of its original value?
SELECT SUBSTR(sc.tgl_faktur, 9, 2) AS tanggal,
SUBSTR(sc.tgl_faktur, 6, 2) AS bulan,
CONCAT(
IFNULL(sc.faktur_beli, ''),
IFNULL(sc.faktur_jual, '')
) AS faktur,
b.id AS kode_bahan,
b.nm_bahan AS nama_bahan,
sc.qty_in AS masuk,
0 AS qty_out
FROM stok_control sc LEFT JOIN bahan b ON sc.id_bahan = b.id