I would like to do this query to include the null result in another calculated field.
SELECT ft.fecha_inicio,
ft.fecha_fin,
IF(ft.fecha_fin is NULL, now(), ft.fecha_fin) fin,
TIMEDIFF(fin,ft.fecha_inicio) total,
IF (ISNULL(ft.fecha_fin), 1, 0) as encurso
FROM fabricaciones_tiempos ft
WHERE ft.id_fabricacion = 138;
is this possible?
You cannot directly use a calculated column as part of another column calculation, but you can do it like this:
SELECT
fecha_inicio,
fecha_fin,
TIMEDIFF(fin,fecha_inicio) total,
encurso
FROM (
SELECT ft.fecha_inicio,
ft.fecha_fin,
IF(ft.fecha_fin is NULL, now(), ft.fecha_fin) fin,
IF (ISNULL(ft.fecha_fin), 1, 0) as encurso
FROM fabricaciones_tiempos ft
WHERE ft.id_fabricacion = 138
) as q;
You can't use the alias but you can either include the original formula in place or assign the value to a variable....
SELECT ft.fecha_inicio,
ft.fecha_fin,
IF(ft.fecha_fin is NULL, now(), ft.fecha_fin) fin,
TIMEDIFF(IF(ft.fecha_fin is NULL, now(), ft.fecha_fin),ft.fecha_inicio) total,
IF (ISNULL(ft.fecha_fin), 1, 0) as encurso
FROM fabricaciones_tiempos ft
WHERE ft.id_fabricacion = 138;
or....
SELECT ft.fecha_inicio,
ft.fecha_fin,
#fin:=IF(ft.fecha_fin is NULL, now(), ft.fecha_fin),
TIMEDIFF(#fin,ft.fecha_inicio) total,
IF (ISNULL(ft.fecha_fin), 1, 0) as encurso
FROM fabricaciones_tiempos ft
WHERE ft.id_fabricacion = 138;
I am trying to use this query, but when there are the same value in different columns, I receive this error:
1060 - Duplicate column name "123"
For example here:
INSERT INTO chiro(in_out,chirocov,chirocov2,chiroded,chiromet,
chirocovp,chirooop,chirooopmet,chirooopcp,
chirovisit,chirouse,chiromax,chirodedapply,
chironum1,chironum2)
SELECT * FROM (SELECT 'in', 'no','individual','123','123','20',
'213','21243','10','14','5','2000','yes',
'0','1') AS tmp
WHERE NOT EXISTS (SELECT in_out,chirocov,chirocov2,
chiroded,chiromet,chirocovp,chirooop,
chirooopmet,chirooopcp, chirovisit,chirouse,
chiromax,chirodedapply,chironum1,chironum2
FROM chiro
WHERE chirocov='no'
AND chirocov2='individual'
AND chiroded='123'
AND chiromet='123'
AND chirocovp='20'
AND chirooop='213'
AND chirooopmet='213'
AND chirooopcp='10'
AND chirovisit='14'
AND chirouse='5'
AND chiromax='2000'
AND chirodedapply='yes'
AND chironum1='0'
AND chironum2='1')
LIMIT 1
But when i change the value, there won"t be any errors. like:
INSERT INTO chiro(in_out,chirocov,chirocov2,chiroded,
chiromet,chirocovp,chirooop,chirooopmet,
chirooopcp, chirovisit,chirouse,chiromax,
chirodedapply,chironum1,chironum2)
SELECT * FROM (SELECT 'in', 'no','individual','123','231','20',
'213','21243','10','14','5','2000',
'yes', '0','1') AS tmp
WHERE NOT EXISTS (SELECT in_out,chirocov,chirocov2,
chiroded,chiromet,chirocovp,chirooop,
chirooopmet,chirooopcp, chirovisit,chirouse,
chiromax,chirodedapply,chironum1,chironum2
FROM chiro
WHERE chirocov='no'
AND chirocov2='individual'
AND chiroded='123'
AND chiromet='123'
AND chirocovp='20'
AND chirooop='213'
AND chirooopmet='213'
AND chirooopcp='10'
AND chirovisit='14'
AND chirouse='5'
AND chiromax='2000'
AND chirodedapply='yes'
AND chironum1='0'
AND chironum2='1')
LIMIT 1
Could you help me and let me know what I am doing wrong?
Just remove the outer SELECT from:
SELECT * FROM (SELECT 'in', 'no','individual','123','123','20',
'213','21243','10','14','5','2000','yes',
'0','1') AS tmp
because it retrieves all the unnamed columns from the inner select with names that are their values, so there are 2 columns with the same name 123.
See a simplified demo of the problem.
Use this:
INSERT INTO chiro(
in_out, chirocov, chirocov2, chiroded, chiromet, chirocovp, chirooop, chirooopmet, chirooopcp,
chirovisit, chirouse, chiromax, chirodedapply, chironum1, chironum2
)
SELECT 'in', 'no', 'individual', '123', '123', '20', '213', '21243', '10', '14', '5', '2000', 'yes', '0', '1'
WHERE NOT EXISTS(
SELECT in_out, chirocov, chirocov2, chiroded, chiromet, chirocovp, chirooop, chirooopmet, chirooopcp,
chirovisit, chirouse, chiromax, chirodedapply, chironum1, chironum2
FROM chiro
WHERE chirocov='no' AND chirocov2='individual' AND chiroded='123' AND chiromet='123' AND chirocovp='20'
AND chirooop='213' AND chirooopmet='213' AND chirooopcp='10' AND chirovisit='14' AND chirouse='5'
AND chiromax='2000' AND chirodedapply='yes' AND chironum1='0' AND chironum2='1'
)
Also LIMIT 1 is not necessary.
I am facing issue during the execution of the following query
UPDATE tbluser_payment
SET payment_date = SUBSTRING(payment_date, 1, 2) + 1
WHERE
trans_updatetime IN (
SELECT
trans_updatetime
FROM
`tbluser_payment`
WHERE
DAY (
cast(trans_updatetime AS date)
) IN ('28', '29', '30', '31')
)
I searched through many questions but get nothing to related with this query
they were inserting record into the database .
I want to update payment_date coloumn with 1 if where clause return true
Is this is possible through this way ?
Any Help Will be Appreciated
Thanks!
I don't think a sub-query is needed here. Use the condition in the where clause.
UPDATE tbluser_payment
SET payment_date = SUBSTRING(payment_date, 1, 2) + 1
WHERE DAY(cast(trans_updatetime AS date)) IN ('28', '29', '30', '31')
Edit: Use if to set the payment_date column in mmyyyy format.
UPDATE tbluser_payment
SET payment_date = IF(length(SUBSTRING(payment_date, 1, 2) + 1) < 2,
'0' + SUBSTRING(payment_date, 1, 2) + 1,
SUBSTRING(payment_date, 1, 2) + 1
)
+ SUBSTRING(payment_date, 3)
WHERE DAY(cast(trans_updatetime AS date)) IN ('28', '29', '30', '31')
and SUBSTRING(payment_date, 1, 2) <> 12
In MySQL, I run a lot of SELECT statements with IN clauses. Sometimes I want to see the output table ordered by the order of values that I used in the IN clause, rather than alpha/numerical.
If I ran
SELECT id FROM x
WHERE id IN ('1', '3', '2', '0')
I would want output 1, 3, 2, 0, not 0, 1, 2, 3.
Is there a way to effect this?
Thanks!
With the following query you should get what you want:
SELECT id FROM x WHERE id IN ('1', '3', '2', '0') ORDER BY FIELD(id, '1', '3', '2', '0')
From the MySQL specification about FIELD():
Returns the index (position) of str in the str1, str2, str3, ... list. Returns 0 if str is not found.
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_field
i am giving query and table column name with datatype.
Column name Data Type
iBasicRate Decimal
iTaxesAndLevies Decimal
vsUnitRate nchar
iAmount Deciaml
vsQuantity Deciaml
First Query Find Unit Rate
Formula is Unit Rate=Basic Rate + Taxes
CASE tBOQ.iBOQLineItemType WHEN '3' THEN Convert(VARCHAR,CONVERT(DECIMAL,tBOQ.iBasicRate)) + Convert(VARCHAR,CONVERT(DECIMAL,tBOQ.iTaxesAndLevies))
WHEN '5' THEN Convert(VARCHAR,CONVERT(DECIMAL,tBOQ.iBasicRate)) + Convert(VARCHAR,CONVERT(DECIMAL,tBOQ.iTaxesAndLevies))
ELSE '---'
END as vsUnitRate,
In this query this is not giving add (addition of amount ) it is just append the next amount.
Example :
Basic rate Tax Unit rate(Basic rate+tax)
4500.00 225.00 4500225 (Show like this)
And Second Query Find Amount
Formula is Unit Rate*Quantity=Amount
CASE tBOQ.iBOQLineItemType WHEN '3' THEN CONVERT(varchar,tBOQ.iAmount)
WHEN '5' THEN CONVERT(varchar,tBOQ.iAmount)
ELSE '---'
END as iAmount ,
Maybe something like this?:
CASE WHEN tBOQ.iBOQLineItemType in ('3', '5') THEN tBOQ.iBasicRate +
tBOQ.iTaxesAndLevies
ELSE 0
END as vsUnitRate,
CASE WHEN tBOQ.iBOQLineItemType in ('3', '5') THEN (tBOQ.iBasicRate +
tBOQ.iTaxesAndLevies) *
tBOQ.vsQuantity
ELSE 0
END as iAmount ,