i have been trying to update a with the number of days using datediff.
The following statement works fine:
select DATEDIFF (date_stock_out,date_stock_in) from paddock
But when I try to update the third field in the table with the following statement, I am getting a syntax error:
update paddock set number_of_days = select DATEDIFF(date_stock_out,date_stock_in) from paddock
Where am I going wrong.
Thanks for any assistance
Normally you do an UPDATE without having a SELECT right in the middle of it:
UPDATE paddock SET number_of_days=DATEDIFF(date_stock_out,date_stock_in)
It's implied you're working on paddock so there's no need to repeat yourself.
SQL statements each have their own unique syntax and restrictions on how, where and when they can be used. Check carefully before composing them together like you would functions in other more flexible programming languages.
I have the following query:
update tblwerknemerdienst toUpdate
set datumtot = (select subdate(datumvanaf,1)
from (select * from tblwerknemerdienst) nextDienst
where nextDienst.Werknemer = toUpdate.Werknemer
and nextDienst.datumvanaf > toUpdate.DatumVanaf
order by DatumVanaf
LIMIT 1)
WHERE DatumTot IS NULL;
The query runs fine on MySql versions other than MySql 5.7.10. I've searched around the web and found that you have to set derived_merge=off, but sadly this had no effect and the query still fails with the same error.
I have also tried several different ways of rewriting the query, but all to no avail.
Is there something I'm missing or is there another way to accomplish this?
In the end I fixed this by rewriting the whole thing in a procedure, where I used a cursor to execute the query and get the necessary data. Then I perform the update statement based upon the fields selected in the cursor.
This seemed to be the only way to reliably perform the operation required on different versions of MySql.
I'm trying to update two tables in MySQL with one query, and am running into an error. I've looked at similar situations and resolutions, but can't seem to translate them to my specific query/situation as I am continuing to get the same error. My goal seems like there is an obvious solution I am missing. My Query is as follows:
UPDATE table_pr, table_pu
INNER JOIN table_pu ON table_pr.id = table_pu.pr_id
SET table_pr.cpr2 = table_pu.cpr2_id, table_pu.cpr_updated = '1'
WHERE table_pu.cpr2_date < CURRENT_DATE()
When I run the Update, I get Not unique table/alias: 'table_pu' returned. I prefer to not use aliases in this, but I've also tried to set unique aliases for the tables with the same result. If my approach should be modified, my ultimate goal is to have table_pr.cpr2 set to table_pu.cpr2_id based on WHERE table_pu.cpr2_date < CURRENT_DATE() and if the update is run, to also set table_pu.cpr_updated = '1'
Any help or guidance would be greatly appreciated.
Removing the second table reference in the UPDATE statement resolved the issue.
This question is to settle an argument between me and a coworker.
Let's say we have the following query, executed on a standard LAMP server.
SELECT field1, field2, field3
FROM some_table
WHERE some_table.field1 = 123
ORDER BY field2 DESC
LIMIT 0, 15
Now let's assume the limit clause is vulnerable to SQL injection.
LIMIT [insert anything here], [also insert anything here]
The point of my coworker is that there is no way to exploit this injection, so there's no need to escape it (since it take more processing power and stuff).
I think her reasoning is stupid, but I can't figure out how to prove her wrong by finding an example.
I can't use UNION since the query is using an ORDER BY clause, and the MySQL user running the query doesn't have the FILE priviledge so using INTO OUTFILE is also out of the question.
So, can anyone tell us who is right on this case?
Edit: the query is executed using PHP, so adding a second query using a semicolon won't work.
The LIMIT clause is vulnerable to SQL injection, even when it follows an ORDER BY, as Maurycy Prodeus demonstrated earlier this year:
mysql> SELECT field FROM user WHERE id >0 ORDER BY id LIMIT 1,1
procedure analyse(extractvalue(rand(),concat(0x3a,version())),1);
ERROR 1105 (HY000): XPATH syntax error: ':5.5.41-0ubuntu0.14.04.1'
Voilà! The above solution is based on handy known technique of so-called error based injection. If, therefore, our vulnerable web application discloses the errors of the database engine (this is a real chance, such bad practices are common), we solve the problem. What if our target doesn’t display errors? Are we still able to exploit it successfully?
It turns out that we can combine the above method with another well-known technique – time based injection. In this case, our solution will be as follows:
SELECT field FROM table WHERE id > 0 ORDER BY id LIMIT 1,1
PROCEDURE analyse((select extractvalue(rand(),
concat(0x3a,(IF(MID(version(),1,1) LIKE 5, BENCHMARK(5000000,SHA1(1)),1))))),1)
It works. What is interesting that using SLEEP is not possible in this case. That’s why there must be a BENCHMARK instead.
I would insert this:
1; DELETE FROM some_table WHERE 1; --
Just after the limit, that will select 1 row from some_table, then DELETE all some_table rows. then the rest will be considered as a comment.
SQL Injection occurs if “externally-influenced input […] could modify the intended SQL command”. And in this case it’s clear that user input can modify the intended SQL command.
However, exploitability is another question. You may not be able to exploit it today. But maybe someday someone is able to exploit it because:
The database connection layer has changed and it is possible to execute multiple statements at once.
The statement has changed and it is possible to use UNION.
The user privileges have changed and it is possible to use INTO OUTFILE/INTO DUMPFILE.
Someone finds a way that you may not have thought of. Have you noticed you can store the result in variables and/or execute a stored procedure?
If you are creating the SQL query from user input, then it will be vulnerable, unless you are using strong typing way before you get anywhere near generating the query. In such a case, there's no way you could make an integer become text saying to drop a table, for example.
Never trust user input, and always perform bounds checking. For example, don't even bother doing the query if the limit values are negative.
You should come with a coding example, not a query example as you do now. She is right on the fact that you cannot really alter a statement since the order by function is always "last". So yes, when you are manually inputting those query's on a sql server, you simply cannot alter the query to output something different rather than a error message. Yet, when you simply add an other query.. you can :)
Finish the last user input as a true value to let the first query run successfully, and add a ';'. Now you can start your own query.
I just have done this on my local mysql server:
SELECT * FROM city order by ID desc limit 0,15; SELECT * FROM city
Even so, in a strong case where there is absolute 0% chance someone could alter the statement, you simply do not even want to receive possible altered data. There should be a reason you use the LIMIT dynamically. Once a person can change your reason, you already have failed. Its not even about risking damage, losing data or what ever. You do not want any manipulation in any way.
In my wordpress multisite, in the main website, I got error when I tried to update any post or widget. When I put my site in debug mode, I found many error with: The table is full for query UPDATE or INSERT as:
WordPress database error The table 'wp_1_options' is full for query UPDATE
WordPress database error The table 'wp_1_comments' is full for query INSERT
etc
And I think each table of my db seem limit contain only 3MB if I checked to the content of those error tables.
I use a database plugin for my wordpress site to check the db, I found most of them got "Overhead" size of 3MB.
What is the exactly issue with above info? How we could solve it?
What does Overhead mean? How can we solve that overhead issue?
The issue solved the same way as stated in the question: ERROR 1114 (HY000): The table is full
I asked my host to change:
innodb_data_file_path = ibdata1:10M:autoextend:max:512M
It solved my problem already.
So What your asking about is actually not possible in MySQL as I recently discovered. A cursor is declared at the top of a stored procedure with the rest of your declarations and MySQL's parse engine cannot manage conditional variable declaration, its all or none.
I came up with the following hack that let me get around this which is that I just declared every cursor I needed separately. I only had two, so this was not crazy:
DECLARE curs CURSOR FOR SELECT DISTINCT(user_id), applied_at FROM application WHERE job_id = JOB_ID_INPUT ORDER BY applied_at DESC;
DECLARE curs_all CURSOR FOR SELECT user_id, applied_at, job_id FROM application WHERE 1 ORDER BY applied_at DESC;
I would suggest that if you plan to have hundreds of conditionally created cursors then you may want to find another way to achieve your task in MySQL, or find a different tool if you cannot. Good luck.
Check this out for more detail:
http://dev.mysql.com/doc/refman/5.0/en/cursors.html