What's wrong with this query (UNION SELECT SUBSTRING) - mysql

I have a query:
select first_name from users where user_id=1
UNION
SELECT IF(SUBSTRING user(),1,4) = 'root',SLEEP(5),1);
Whenever I run it I get the following error:
ERROR 1064 (42000): 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 'user(),1,4) = 'root',SLEEP(5),1)' at line 1
My purpose is to test whether the user is root.
SUBSTRING user(),1,4) means: starting from position 1, get four characters (so basically root) .If the database user is root then pause for 5 seconds.
But SLEEP(5),1) what does it mean apart from instructing to pause for the specified 5 sec?
Thanks a lot

The right sintax is:
IF(SUBSTRING(user(),1,4) = 'root',SLEEP(5),1)
you are just missing a (. Maybe you can also use this:
IF(user() like "root%", SLEEP(5), 1).
Sleep(n) just pauses the execution of the query for n seconds. I don't find it really useful... but it's possible to do.

Related

A query on the recipesexample database from SQL for mere mortals

What's the problem with this query?
It's only a slight modification from the SQL for mere mortals book...
Select r.RecipeTitle,
from (Select RecipeClassID
from recipe_classes as RC
where RC.RecipeClassDescription like "Main%"
or RC.RecipeClassDescription="Dessert") as rcfiltered
inner join recipes as r
on rcfiltered.RecipeClassID = r.RecipeClassID;
Remove the comma:
Select recipes.RecipeTitle,
from ...
There must be no comma following the last expression in the select-list.
WRONG:
SELECT A, B, C, FROM ...
RIGHT:
SELECT A, B, C FROM ...
With the extra comma, the query produces this error in MySQL:
ERROR 1064 (42000): 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 'from (Select RecipeClassID
from recipe_classes as RC
where RC.Recipe' at line 2
Here's a tip on how to read syntax error messages: It tells you exactly where the parser got confused: "near 'from ..." This probably means something you wrote in the query immediately prior to that position was wrong. Knowing this helps to narrow down the cause of the syntax error.
If the error says, "near ''" then it means it got to the end of the query and then got confused. Maybe you opened a parenthesis but forgot to close it for example.
That's the first problem that jumps out at me. I don't know the original query you were modifying, so I don't know how you changed it. I don't know if the query does what you intend it to do, but aside from the comma issue it looks like the syntax is valid.

Unusual error in mysql "delete" query

I have the following mysql code which I run in a shell script:
mysql -u "$DB_USER" -p"$DB_PASSWD" "$DB_SOURCE" << MYSQLEOF
DELETE FROM list_subscriber_events WHERE subscriberid IN
(SELECT subscriberid FROM
(SELECT subscriberid FROM `list_subscriber_events` WHERE subscriberid NOT IN
(SELECT subscriberid FROM list_subscribers)) AS c);
MYSQLEOF
When running the delete query from the command line in mysql I have no problems - it runs ok.
However when running it in the bash script I get the following error:
ERROR 1064 (42000) at line 4: 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 'WHERE subscriberid NOT IN
(SELECT subscriberid FROM list_subscribers)) AS c)' at line 3
ssh exited with exit code 1
I've spent hours trying all sorts of variations - I can't seem to resolve it. Any help would be appreciated - I'm probably missing something obvious.
Try removing the backtick characters
FROM `list_subscriber_events` WHERE
^ ^
In the context of a shell command line, those backticks indicate something that needs to be run, and the return from that is substituted.
I suspect that the shell is interpreting those backticks, and attempting to execute list_subscriber_events, which is likely returning an error. The result is that this gets replaced with an empty string... the resulting statement is
... FROM WHERE ...
If you have to include backticks, likely preceding the backtick with a backslash character will escape it.
FOLLOWUP
On a totally different note, it seems like you have more inline views that are necessary. And there's a potential "gotcha" with the NOT IN, if the subquery returns a NULL value. (Maybe you have a guarantee that subscriberid is NOT NULL in list_subscribers. Or, maybe you do want the behavior we get when that subquery returns a NULL value.)
Seems like a much simpler query, using an anti-join pattern would accomplish the result you seem to be after... to remove the rows from list_subscriber_events where the subscriberid value does not appear in list_subscribers
DELETE e.*
FROM list_subscriber_events e
LEFT
JOIN list_subscribers l
ON l.subscriberid = d.subscriberid
WHERE l.subscriberid IS NULL

SQL trimming syntax error

I've just finished 'SAM's Teach Yourself SQL in 24 Hours' book, and I've been given the task of listing which of our active site's "site_code" match their respective "site_path"s with the prefix '/var/www/html' removed. I've been stuck for the past hour or so trying to figure out what I'm doing wrong and don't know what else to try at this point.
This is what I've got so far:
SELECT site_name FROM example_tbl
WHERE active = 1
AND site_code IN
(SELECT TRIM(LEADING '/var/www/html/' FROM site_path) FROM example_tbl;);
Trying to run that's giving me this:
ERROR 1064 (42000): 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 4
ERROR 1064 (42000): 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 1
I'm using Mysql ver 14.14 Dustrib 5.5.38 on Ubuntu ver 14.10
The nested query works fine by itself, and if I get rid of everything from 'AND' on, the first part works fine as well, so I'm not sure why they won't work together :\
Any help would be much appreciated!
EDIT:
Sorry, I should have been more specific!
'site_code', 'site_path' and 'site_name' are all columns in the table 'example_tbl', and I'm trying to get a list site names to print out like this
Input: /var/www/html/example.company.com
Output: example.example.com
EDIT2:
Oh and for that example above:
The 'site_name' would be: example.company.com
The 'site_code' would be: example
And the 'site_path' would be: /var/www/html/example.company.com
Remove the extra semicolon.
SELECT site_name FROM example_tbl
WHERE active = 1
AND site_code IN
(SELECT TRIM(LEADING '/var/www/html/' FROM site_path) FROM example_tbl);
The following query uses the MySQL SUBSTRING function instead of TRIM:
SELECT site_name
FROM example_tbl
WHERE active = 1 AND
SUBSTRING(site_path, 1, 13) = '/var/www/html' AND
SUBSTRING(site_path, 14) = site_code
I don't think you needed a subquery to solve your problem. Also, I included a condition in the WHERE clause to also check that the site_path begins with 'var/www/html'. If you expect all your sites to begin with this, then feel free to remove this condition.

Syntacts error in mysql query

Someone gave me this query to delete community from a database on my server using phpMyAdmin, it worked when he use it so I asked him to send it to me, but I get a error
MySQL said:
Documentation
#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 '"SELECT * FROM `connections` WHERE 1" :
delete from connections where communit' at line 1
I did a search on the error but could not figure it out.
"SELECT * FROM `connections` WHERE 1" :
delete from connections where community="XYZ"
You should separate your queries with ; and not :
The error you are receiving as you've shown it is because this line is incorrect:
SELECT * FROM `connections` WHERE 1"
First because it's ending in an unnecessary double quotation mark. MySQL does not use double quotes but single quotes, and even still the other single quote is not there to match it.
The single quotes are also a problem for community="XYZ", this should read: community = 'XYZ'
Secondly, you don't have a condition for your where statement, you must be missing something like:
WHERE columnName = 1;
If you were trying to select everything from connections, you can just remove that where clause all together.
EDIT
In addition, MySQL queries are separated by a semi-colon, not a colon, so MySQL will not realize you are trying two different queries.

1064 MySQL error in decrement using

I'm using Update videos Set views = views + 1 Where video_id='$id', but MySQL give me back error 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 ' 8' at line 1
What can cause it?
Most likely $id is not what you expect it is. I imagine the query that is coming through looks something like
update videos set view = views + 1 where video='' 8'';
Note: Those are two single quotes on either side of the 8.
To confirm this you have a couple options.
Turn on general query logging, as a super user (root) from the mysql command prompt run
set general_log_file='/tmp/mysql.log';
set general_log ='on';
Now every single query that gets sent to mysql will show up in /tmp/mysql.log (Note this can quickly grow very large so don't leave it on after you're done debugging).
App logs
Do you have any kind of logging frame work going on? Before your actual call to execute the query, log the value of ($id). For a poor mans logging you could do something like
file_put_contents('/tmp/debug.txt', date("Y-m-d H:i:s")." id is [$id]\n",FILE_APPEND);