Error code 1093:Error in Sql Syntax - mysql

I have the following sql .
update voting_table
Set VOTING_STATUS = 1
where CE_ID = 15813
and
VOTING_PK =
(SELECT VOTING_PK FROM voting_table
ORDER BY VOTING_PK DESC
LIMIT 1) ;
But the editor shows me the following error :
Error code 1093, SQL state HY000: You can't specify target table 'voting_table' for update in FROM clause
How can I overcome the error ? Please help me .

I think these SQL will helpful to you.
update voting_table as table_1, (SELECT VOTING_PK FROM voting_table ORDER BY VOTING_PK DESC LIMIT 1) as table_2
where table_1.VOTING_PK = table_2.VOTING_PK and CE_ID = 15813
Set VOTING_STATUS = 1
Thank you.

Related

Update query in SQL: command not ended properly

update fare_strategy set sales_date_to = ’07-SEP-22’
where dep_date_to is not null
and market_id in
(select market_id
from fare_strategy_markets
where set_type = ‘STANDARD’
);
In the following query, I'm getting error as SQL command not properly ended.
try to change like this because in your query you used ’ this is not valid so i just change it to ' so it's works fine
update fare_strategy set sales_date_to = '07-SEP-22' where dep_date_to is
not null and market_id in (select market_id from
fare_strategy_markets where set_type = 'STANDARD');

Updating the last row MYSQL

I want to update the last row in a mysql table
UPDATE logend
SET endsecs = endsecs+'$moretime'
WHERE id = (SELECT id FROM logend ORDER BY id DESC LIMIT 1)
But it doesn't work, because of this error:
ERROR 1093 (HY000): You can't specify target table 'logend' for update in FROM clause
In MySql you can't use the updated table in a subquery the way you do it.
You would get the error:
You can't specify target table 'logend' for update in FROM clause
What you can do is an UPDATE with ORDER BY and LIMIT:
$sql = "UPDATE logend SET endsecs=endsecs+'$moretime' ORDER BY id DESC LIMIT 1";
Can't you just get the max(id) and update that?
$sql = "UPDATE logend SET endsecs=endsecs+'$moretime' WHERE id = (
SELECT id FROM (
SELECT MAX(id) FROM logend) AS id
)";
Here's another solution: a self-join, to find the row for which no other row has a greater id.
Also, you should really not interpolate POST inputs directly into your SQL statement, because that exposes you to SQL injection problems. Use a query parameter instead.
$moretime = $_POST['moretime'];
$sql = "
UPDATE logend AS l1
LEFT OUTER JOIN logend AS l2 ON l1.id < l2.id
SET l1.endsecs = l1.endsecs + ?
WHERE l2.id IS NULL";
$stmt = $mysqli->prepare($sql);
if (!$stmt) {
trigger_error($mysqli->error);
die($mysqli->error);
}
$stmt->bind_param("s", $moretime);
$ok = $stmt->execute();
if (!$ok) {
trigger_error($stmt->error);
die($stmt->error);
}

MySQL error : sql_mode=only_full_group_by

i have this script where i'm used to do a simple projection for a dynamic result
SELECT
M.nom_utilisateur,
SUM(M.montant_bulletin ) as Montant_Total_BS,
SUM(M.montant_payer ) as Montant_Total_payer,
COUNT(M.ref_bs ) as nbr_bs_total,
(SELECT COUNT(*) FROM mutuelle_bi.`Mutuelle` WHERE nom_utilisateur = M.nom_utilisateur AND (M.nom_assurence = "Star" AND M.etat_bs = "Remboursé")) as nbr_bs_total_payer,
(SELECT COUNT(*) FROM mutuelle_bi.`Mutuelle` WHERE nom_utilisateur = M.nom_utilisateur AND (M.nom_assurence = "Star" AND M.etat_bs = "Non remboursé")) as nbr_bs_non_payer,
(SELECT COUNT(*) FROM mutuelle_bi.`Mutuelle` WHERE nom_utilisateur = M.nom_utilisateur AND (M.nom_assurence = "Star" AND M.etat_bs = "En cours")) as nbr_bs_en_cours,
(SELECT COUNT(*) FROM mutuelle_bi.`Mutuelle` WHERE nom_utilisateur = M.nom_utilisateur AND (M.nom_assurence = "Star" AND M.etat_bs = "Nouveau")) as nbr_bs_nouveau
FROM mutuelle_bi.`Mutuelle` M
WHERE M.nom_assurence = "Star"
GROUP BY M.nom_utilisateur
but something goes wrong ; as i have this error :
1055 - Expression #5 of SELECT list is not in GROUP BY clause and
contains nonaggregated column 'mutuelle_bi.M.etat_bs' which is not
functionally dependent on columns in GROUP BY clause; this is
incompatible with sql_mode=only_full_group_by
How may i modify my script by the way to solve this problem ,
given that i shouldn't modify any config file of my sql server . and i should only write scripts
Any Suggestions ??
You can also try to disable the only_full_group_by setting:
set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
Worked for me.
I think you just want conditional aggregation:
SELECT M.nom_utilisateur,
SUM(M.montant_bulletin ) as Montant_Total_BS,
SUM(M.montant_payer) as Montant_Total_payer,
SUM(M.etat_bs = 'Remboursé') as nbr_bs_total_payer,
SUM(M.etat_bs = 'Non remboursé') as nbr_bs_non_payer,
SUM(M.etat_bs = 'En cours') as nbr_bs_en_cours,
SUM(M.etat_bs = 'Nouveau') as nbr_bs_nouveau
FROM mutuelle_bi.`Mutuelle` M
WHERE M.nom_assurence = 'Star'
GROUP BY M.nom_utilisateur;

MySQL SELECT syntax error that I am not able to identify

I am trying a simple query in mysql and I am getting a syntax error that I need help understanding.
SELECT
eea.*,
ee.description,
eect.title,
eect.file,
eect.location,
eect.img_location
FROM
`e_exam` ee,
`e_exam_attempt` eea,
`e_exam_cert_template` eect
WHERE
eea.a_user_id = 1,
eea.ee_id = ee.id,
ee.eect_id = eect.id;
I am getting the following error:
Basically the syntax error on line 13
eea.ee_id = ee.id, ee.eect_id = eect.id LIMIT 0, 25
Anyone have idea how I can edit this to get the -1 vote to improve?
Its a simple syntax error, your WHERE clause should not be seperated by commas. Use AND or OR etc
SELECT
eea.*, ee.description, eect.title, eect.file,
eect.location,eect.img_location
FROM
`e_exam` ee,
`e_exam_attempt` eea,
`e_exam_cert_template` eect
WHERE
eea.a_user_id = 1 AND
eea.ee_id = ee.id AND
ee.eect_id = eect.id
LIMIT 0,25
You should also learn about the JOIN syntax
SELECT
eea.*, ee.description, eect.title,eect.file,
eect.location, eect.img_location
FROM `e_exam_attempt` eea
JOIN `e_exam` ee ON eea.ee_id = ee.id
JOIN `e_exam_cert_template` eect ON ee.eect_id = eect.id
WHERE
eea.a_user_id = 1
LIMIT 0,25

"Invalid use of group function" when insert on duplicate key update

I have trouble with my query which I want to insert and update while it is duplicate, but it said "Invalid use of group function" instead.
I have run my only "Select" statement and there was no issue like "Invalid use of group function".
here is my full code :
INSERT INTO tbl_biir_aktual(cabang_kode, periode_thn, periode_bln, pending_pp_volume, pending_pp_value)
SELECT a.cabang_kode, YEAR(a.tanggal) AS tahun, MONTH(a.tanggal)AS bulan,
SUM(a.qty_pending*a.unit_barang)AS tonase_pending, SUM(a.value_pending)AS value_pending
FROM tbl_order a,
(SELECT b.cabang_kode, MAX(b.tanggal)tanggal
FROM tbl_order b
GROUP BY b.cabang_kode, YEAR(b.tanggal), MONTH(b.tanggal)) AS max_cabang
WHERE max_cabang.cabang_kode = a.cabang_kode AND max_cabang.tanggal = a.tanggal
GROUP BY cabang_kode, YEAR(tanggal), MONTH(tanggal)
ON DUPLICATE KEY
UPDATE pending_pp_volume = SUM(a.qty_pending*a.unit_barang), pending_pp_value = SUM(a.value_pending);
well,
Hey I just found this MySQL ON DUPLICATE KEY UPDATE while inserting a result set from a query
INSERT INTO tbl_biir_aktual(cabang_kode, periode_thn, periode_bln, pending_pp_volume, pending_pp_value)
SELECT a.cabang_kode, YEAR(a.tanggal) AS tahun, MONTH(a.tanggal)AS bulan,
#tonase_pending := SUM(a.qty_pending*a.unit_barang)AS tonase_pending, #value_pending := SUM(a.value_pending)AS value_pending
FROM tbl_order a,
(SELECT b.cabang_kode, MAX(b.tanggal)tanggal
FROM tbl_order b
GROUP BY b.cabang_kode, YEAR(b.tanggal), MONTH(b.tanggal)) AS max_cabang
WHERE max_cabang.cabang_kode = a.cabang_kode AND max_cabang.tanggal = a.tanggal
GROUP BY cabang_kode, YEAR(tanggal), MONTH(tanggal)
ON DUPLICATE KEY
UPDATE pending_pp_volume = #tonase_pending, pending_pp_value = #value_pending;
I've tried that and it's done.
Tq Edper for your comment anyway....
Try using the alias in your update:
UPDATE SET pending_pp_volume = tonase_pending, pending_pp_value = value_pending
Also it's more wise to use another name for your alias for your aggregate than using the same name as your existing field name like the value_pending. Might as well changed it to TotalValuePending, so that it would be:
UPDATE SET pending_pp_volume = tonase_pending, pending_pp_value = TotalValuePending