I am trying to select students within a MYSQL database where their dates of birth are in certain months (May, June or July) here is what I think the command is:
SELECT studentid, fName, lName, dob
WHERE MONTH(dob)='5'
FROM student;
Here is an example of a records:
However I put this in the PHPmyadmin sql box and it returns an 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 'WHERE MONTH(dob)='5' FROM student LIMIT 0, 30' at line 2
The field type is date. MySQL version is 5.1.41.
Any help will be appreciated.
Thanks
Move the FROM before the WHERE:
SELECT studentid, fName, lName, dob
FROM student
WHERE MONTH(dob)='5'
Related
In my SQL table I have "country" and "we200326" columns. Column "we200326" contains only "1" or "NULL" entries.
I'm trying to get a total of all "1"s in column "we200326" and a total by country. I have written the following statement but it gives an error but I don't know what I did wrong (I'm very new at this):
SELECT country, we200326,
(SUM(we200326) OVER () AS Total)
(SUM(we200326) OVER (PARTITION BY country) AS CountryTotal)
FROM table_name
ORDER BY CountryTotal, Country;
The error I get is this:
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 'OVER () AS Total)
(SUM(we200326) OVER (PARTITION BY country) AS CountryTotal)
' at line 2
I have searched for similar errors and found several (each time was a simple syntax error like a space or comma or so) I tried several versions but could not resolve my problem when following those instructions. Any help would be appreciated.
Window functions are available in MySQL 8.0 only.
In earlier versions, one option is to use subqueries:
select
country,
wewe200326,
(select sum(we200326) from table_name) total,
(select sum(we200326) from table_name t1 where t1.country = t.country) country_total
from table_name t
order by country_total, country
I just moved to mySQL 8 and have errors on queries that worked in mySQL 5 before.
I can get the query to work without the string matching part, where it just matches an interger field, but it errors when I add the string part.
Query: select * from crew where webvisible = 1 and active = 1 and rank = 'member'
Error: 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 '= 'member'' at line 1
I need to query based on the rank field where it is different string values like: member or member applicant, or stowaway, etc.
rank is a reserved word in MySQL 8+. You need to enclose it in backticks i.e.
select * from crew where webvisible = 1 and active = 1 and `rank` = 'member'
i have a table and i have columns. I want to get AGE column- it is creating (current date - year column data)- and whole columns like *.
I tried;
SELECT (YEAR(CURRENT_DATE) - YEAR(dateOfBirth)) as age , * FROM users
but it gives 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 '* FROM users LIMIT 0, 25' at line 1
What should i write? Thank you!
use alias and TIMESTAMPDIFF function
SELECT u.*,
TIMESTAMPDIFF(YEAR, dateOfBirth, CURRENT_DATE) as age
FROM users u
'Use of an unqualified * with other items in the select list may produce a parse error. To avoid this problem, use a qualified tbl_name.* reference' - https://dev.mysql.com/doc/refman/8.0/en/select.html
This is ok
SELECT (YEAR(CURRENT_DATE) - YEAR(dateOfBirth)) as age, users.* FROM users
SELECT (SUM(MAX(identifier)+1) FROM student_ids WHERE state ='state' AND year='year');
SELECT (SUM(MAX(identifier)+1) FROM student_ids WHERE state ='state' AND year='year')
LIMIT 0, 25
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 'FROM student_ids WHERE state ='state' AND year='year')
LIMIT 0, 25' at line 1
how can i fix the error
The SUM function is for taking aggregates of columns, across multiple rows. You don't need to use it to add a scalar value:
SELECT MAX(identifier) + 1 AS max_id
FROM student_ids
WHERE state = 'state' AND year = 'year';
Additionnaly to #tim-biegeleisen, use ` for column names and table names, like this : `year`, `state`, ...
Because some words you uses are reserved. YEAR is reserved word and could return an error if you uses it as column name. SQL thinks you are calling the year function instead of a column named year.
SELECT MAX(`identifier`) + 1 AS `max_id`
FROM `student_ids`
WHERE `state` = 'state'
AND `year` = 'year' ;
Here is documentation about reserved words in mySQL :
https://dev.mysql.com/doc/refman/5.5/en/keywords.html
I am beginner to mysql, while I practice syntax of LAST() i.e
SELECT LAST(CustomerName) AS LastCustomer FROM CUSTOMERS; I am getting an error.
Error : ERROR 1064 (42000): 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 '(CustomerName) AS LastCustomer FROM
CUSTOMERS' at line 1
the thing is there is nothing on mysql like LAST() function. You can see the comment here where it is said.
You can find the list of mysql functions visiting mysql aggregated functions
It is better to use simple query like the following -
SELECT CustomerName as LastCustomer ORDER BY id DESC LIMIT 1
Hope it helps...:)
Last() is not supported in Mysql so try
SELECT TOP 1 CustomerName FROM Customers ORDER BY CustomerID DESC;
try this
SELECT * FROM CUSTOMERS ORDER BY customer_id DESC LIMIT 1;