Why is this SQL delete statement not working? - mysql

I have a SQL delete statement. The syntax works and the values are all correct, but allways when I execute it, it ask me if I really want to delete 0 rows.
delete * from database1 where (database1.Name = 'Alain' and database1.Grund = 'Defekt' and database1.Gerätename = 'Beamer' and database1.Ausleihdatum = '9/2/2019' and database1.Rückgabedatum = '15/3/2019')
I don't get an error or something else, it just ask me if I want to delete 0 rows, but that makes no sense, because the values are all correct. I checked them like 5 times. I tried to make the statement without the *,
delete from database1 where (database1.Name = 'Alain' and database1.Grund = 'Defekt' and database1.Gerätename = 'Beamer' and database1.Ausleihdatum = '9/2/2019' and database1.Rückgabedatum = '15/3/2019')
but that doesn't work too. Here it is the same scenario...
I wan't that the delete statement works the right way, but I don't know how.

The mistake was a relation that I recently added to an other table. So the "Gerätename" value was still the right one in the table I wanted to delete something, but not in the other Table...

Related

Statement in trigger is not "picking up" the condition in its Where clause

so I'm currently working on a MySQL trigger. I'm trying to assign values to two variables when a new record is inserted. Below are the queries:
SET mssgDocNo = (SELECT Document_ID FROM CORE_MSSG WHERE Message_ID = new.MSSG_ID);
SET mssgRegime = (SELECT CONCAT (Regime_Type, Regime_Code) FROM T_DOC WHERE CD_Message_ID = new.MSSG_ID);;
For some reason, the second SQL query is not picking up the 'new.MSSG_ID' condition while the first query in same trigger recognizes it. I really can't figure out what seems to be the problem.
When I replace the 'new.MSSG_ID' with a hard-coded value from the database in the second query it seems to work. I doubt the 'new.MSSG_ID' is the problem because it works perfectly fine in the first query.
I've tried pretty much anything I could think of. Would appreciate the help.
I would write these more simply as:
SELECT mssgDocNo := Document_ID
FROM CORE_MSSG
WHERE Message_ID = new.MSSG_ID;
SELECT mssgRegime := CONCAT(Regime_Type, Regime_Code)
FROM T_DOC
WHERE CD_Message_ID = new.MSSG_ID;
The SET is not necessary.
I did make one other change that might make it work. I removed the space after CONCAT. Some versions of MySQL have trouble parsing spaces after function calls.

Update a column on a table from a table on another schema MySql

I accidentally updated far more records than I should have with the wrong information. I have the correct information in another schema but I cannot figure out how to update the incorrect one.
I've tried something like -
Insert into schema1.table1 set columnName = (Select statement????) Where ?????
And then I get lost.
I've read around and tried a few answers out and I keep either getting a syntax error, or nothing happens.
Any help would be appreciated.
update schema1.table1
inner join schema2.table2
on schema1.table1.IDColumn = schema2.table2.IDColumn
set schema1.table1.column1 = schema2.table2.column2
This Worked for me

MySQL Error 1172 - Result consisted of more than one row

I'm getting this error from MySQL when running a query inside a stored procedure:
Error Code: 1172
Result consisted of more than one row
I understand the error: I'm doing a SELECT (...) INTO (var list), and thus the query is required to return a single row. When I use LIMIT 1 or SELECT DISTINCT, the error goes away.
However: when I run the original query manually (without LIMIT or DISTINCT), it does return a single row. So I'm suspecting I may have bumped into a MySQL bug. Does anyone know what could be happening?
EDIT
I'm posting the SQL as requested. Everything that starts with an underscore is a variable declared earlier inside the procedure. When I test it, I'm replacing _cd_pai_vc with the ID for the record that is causing the problem.
SELECT a.valor, IFNULL(p.valor, 0), fn_cd2alias(ra.cd_registro), fn_cd2alias(IFNULL(p.valor,0))
INTO _valor, _cd_pai_vc, _alias_verbete, _alias_pai
FROM dados_registros ra
INNER JOIN dados_varchar255 a
ON a.cd_registro = ra.cd_registro
AND a.fl_excluido = 0
AND a.alias = 'vc-verbetes-termo'
LEFT OUTER JOIN dados_registros rp
INNER JOIN dados_int p
ON p.cd_registro = rp.cd_registro
AND p.fl_excluido = 0
AND p.alias = 'vc-remissoes-termo referenciado'
INNER JOIN dados_int pt
ON pt.cd_registro = rp.cd_registro
AND pt.fl_excluido = 0
AND pt.alias = 'vc-remissoes-tipo remissao'
AND fn_cd2alias(pt.valor) = 'hierarquica'
ON ra.cd_registro = rp.cd_entidade
AND rp.fl_excluido = 0
AND fn_cd2alias(rp.cd_modulo) = 'vc-remissoes'
WHERE ra.cd_registro = _cd_pai_vc
AND ra.fl_excluido = 0;
I had the similiar issue and when I put table alias it worked like a charm.
SELECT t.tax_amount,t.tax_percentage FROM nepse_tax t
I had this problem and found it went away when I used both table name and column name in select statements, even simple ones.
The issue i had was IN parameter and the column name both were same so altered IN parameter name and it worked
I have experienced the same error in mysql.
MySQL Error 1172 - Result consisted of more than one row
Then I saw the question:
mysql stored procedure error (1172, 'Result consisted of more than one row')
But it was not I meant to ask. LIMIT 1;-wasn't up to my expectation. It will just return the first row for all the case.
Then I started looking at this one deeply and now I got the solution.
This case happend because the code for stored procedure returns multiple rows and that was because I had many extra spaces and tab characters in my code[the code I wrote for stored procedure] and when I removed them with just one/two appropriate tab chars- it was just like a flying machine.
I don't know if it is the same case that you are experienced with. Anyway give a try.
Thank you.
I had the same problem, the solution by Nava Bogatee worked like a charm,
below is a part of the procedure
select attendance
into brb
from Stud
where Rno like rno;
This didn't work I have no idea why.
Then I changed this to
select t.attendance
into brb
from Stud t
where t.Rno like rno;
and it worked.
I guess due to some reason the Rno column is returning all the values in the table, that is why the multiple row error.

UPDATE query not functioning in MySQL 5.0

I'm not completely sure what I'm doing wrong here I've looked through again and again for incorrectly placed quotes but I cannot for the life of me get this UPDATE query code to work... and I've troubleshooted it down to the query itself and it gives an incorrect syntax error, yet I have no idea what is wrong with it as it is identical to the manual.
$change = mysql_query("UPDATE Images SET Group='$group' WHERE ID = '$imgid'") or
die(mysql_error());
you might need to do :
"UPDATE Images SET `Group`='$group' WHERE ID = '$imgid'"
as group is a keyword in MySQL .
Well... if the update does not work this might have two reasons. Either your syntax is wrong; this should result in MySQL throwing an error which should show up with your die(mysql_error()).
Or there is simply no record matching your WHERE-condition. To check if any record was updated at all you might want to take a look at mysql_affected_rows(). And you could run a SELECT-query on that table, using the same WHERE-condition.
try this ..
"UPDATE Images SET Group='".$group."' WHERE ID = ".$imgid

is this the correct way to optimize MySQL Query?

$sql="update users_contain
set
wood_max = (Select building_production from building_level where merge_id=$subPrimaryKey and empire_id=$user_empireID) ,
iron_max = wood_max,
clay_max = wood_max
where user_id = $user_id";
Now there is a question.
will wood_max will always be updated first than iron_max and clay_max. so it is safe to use this way??
i do not want to use inner query for updating the iron_max and clay_max when i know it has same value for all three fields..
According to this documentation, your UPDATE statement works as you want it to: http://dev.mysql.com/doc/refman/5.1/en/ansi-diff-update.html
Test it to be sure, but I think you're fine.