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.
Related
I am having a hard time getting the value in the database with a constraint of getting only the first 4 letters of the name as well as its in uppercase.
I am using MySQL on Command Prompt and so far I have tried this syntax and I always get this error,
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM STUDENTS' at line 1
The syntax I have tried;
SELECT UCASE(MID(NAME,1,3) FROM STUDENTS;
SELECT UPPER(SUBSTRING(NAME,1,3) FROM STUDENTS;
Tried all of them but I am still getting errors. Do you guys have any idea where I might be wrong?
you have just missed a closing parenthesis.
SELECT UPPER(SUBSTRING(NAME,1,3)) FROM STUDENTS;
Also, to get the first 4 letters you should use SUBSTRING(NAME,1,4)
Cheers
both of queries that you used have wrong parenthesis match (every opening parenthesis need closing one). If you want to get first 4 letters you should replace 3 with 4
SELECT UCASE(MID(NAME,1,4)) FROM STUDENTS;
SELECT UPPER(SUBSTRING(NAME,1,4)) FROM STUDENTS;
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.
I am getting an error that I dont know how to deal with.
I am running the same code without issue for another column but for this column it refuses to work.
SELECT * FROM Players WHERE Character = 'momo' // This one wont work
SELECT * FROM Players WHERE Class = 'Fighter' // this one works
Character is a VARCHAR and Class is TEXT. I have tried changing Character to TEXT and I still get the same issue. The value 'momo' exists in the table.
ERROR: Couldn't connect to server. SQLSTATE[42000]: Syntax error or access violation: 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 '= ''' at line 1
Edit:
I am editing this incase someone find this and wants to know how it was fixed. User by the name of ueerdo Pointed out that I should use quotations and when I did, it worked. So I started looking into why it happened and I found out the SQL reserves Character for something else so it is something that I can't use unless it is in quotations.
It is best to delimit identifiers to prevent possible collision with reserved words and keywords.
SELECT * FROM `Players` WHERE `Character` = 'momo'
I'm not to sure why this is happening and i've been trying to figure it out now for a while.
I've got the follow code
SELECT (MAX(replace(replace(replace(`sku`,'PA1-',''),'TES-',''),'BOX-',''))+1) AS maxValue FROM `product` WHERE `sku` LIKE '%PA1-TES-BOX%'
This was working a while back and nothing has changed code wise, I can only assume that a server changes has caused this to return the following 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 'maxValue FROM ``product`` WHERE ``sku`` LIKE '%PA1-TES-BOX%'
Basically this SQL was built to find the first 3 section of this SKU code and return the ending + 1 so 002 would then return 003 to ensure unique sku codes.
Maybe the replace function has changed, i'm not entirely sure.
Does anyone have any ideas why this suddenly is throwing the error above?
I don't see an obvious syntax error. But assuming the number is the last hyphenated item in the sku, the code could more easily be written as:
select (substring_index(sku, '-', -1) + 1) as maxvalue
. . .
One possibility for the syntax error is that an unprintable character crept in around the as.
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.