I'm trying to do a simple query (below) to see if there are no rows found in a table. If there are none, then do something.
SELECT COUNT (*) as count FROM AT_vbc_content_status
However, I keep getting this 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 '*) as count FROM [name of my table] WHERE member_id= '16'' at
line 1
Where am I going wrong?
You have to remove the space between COUNT and (
COUNT(*)
Remove the space between COUNT and the parenthesis like this: COUNT(*)
See the MySQL documentation at the link below for explanation
http://dev.mysql.com/doc/refman/5.1/en/function-resolution.html
The MySql function is COUNT(field_name) and there must not be any space between COUNT and (*), So Remove the extra space between COUNT and (*)
$content_status_sql = "SELECT COUNT(*) as count FROM AT_vbc_content_status WHERE member_id= '".$_SESSION['member_id']."'";
Related
What is the difference between these two MySQL statements?
Works:
select *, count(mycol) c from mytable group by mycol;
Doesn't work:
select count(mycol) c, * from mytable group by mycol;
The first statement works as I'd expect, while the second one gives me a syntax error. Why does the order matter?
I'm having trouble finding an answer from Google, because I'm not entirely sure if I'm asking the question correctly.
Edit:
Here's the sanitized error message. I'm using MySQL Workbench, if that's relevant.
Error Code: 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 mytable group by id' at line 1
Just alias the table and the syntax error will go away.
select count(t.id) c, t.* from mytable t group by id;
See this db fiddle.
It looks like MySQL allows bare (unqualified) * only as immediatly following SELECT. The following query also raises a syntax error :
select 1, * from mytable t;
The documentation prevents against using bare * combined with other items in the SELECT list :
A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables.
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.
So, today I tried doing the following:
SELECT count(users), * FROM table;
And it gave me a syntax error on the , after count(users), but this:
SELECT *, count(users) FROM TABLE;
Or this:
SELECT count(users), users FROM table;
Does work. Is there a reason for that?
Edit: Per request, here's the error:
Error Code: 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 mygamedb' at line 1
MySQL Workbench underlines the comma after the count(*). I'd like to point out I know it's an ugly select, I should group by and such, but the question is not about how to make it work, but a short example asking about why does that happen, not how to make it work.
You could use alias to make it work:
SELECT count(users), t.*
FROM table t;
DBFiddle Demo
SELECT count(users), *
FROM table;
-- 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 tablez' at line 1
I am trying to explain you when you will use this SELECT count(users), * FROM table; query then mysql query fetch table as like
Table :
---count(users)---
result of count
Then you are adding * means fetch all attribute value but now mysql check * from fetch table but that is not their because table has only one attribute that name is count(users) that's why it show error. but when you will use t.table (t is alias of table) that fetch from table and it works fine.
I am commenting here what is my understanding.
Thanks
I found this post:
Using an Alias in SQL Calculations
which suggests you can use an alias in calculations by using
(select alias)
like:
SELECT 10 AS my_num,
(SELECT my_num) * 5 AS another_number
FROM table
This works fine. But now I want to use an alias in an if. So I thought it might work the same way. So I tried:
SELECT 10 AS my_num,
IFNULL(otherfield, (SELECT my_num)) AS another_number
FROM table
which doesn't work at all, telling me
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 '(SELECT my_num)) AS another_number
Is there any way to make this work in MySql?
No, use the entire expression directly like below unless you are accessing it in a outer query.
IFNULL(otherfield, 10) AS another_number
In your case it should be
SELECT 10 AS my_num,
IFNULL(otherfield, 10) AS another_number
FROM table
I am executing this query in MySql:
SELECT amount
FROM Prices
WHERE (item_id = 1246 AND
('2016-12-26' BETWEEN (effective_date AND COALESCE(end_date, NOW()))))
But for some reason I get a syntax error that I don't see where it is.
the error is:
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 ')) AND(item_id = 1419 AND ('2017-01-14' BETWEEN (effective_date AND COALESCE(end' at line 1
the Price is like this:
Prices
id
item_id
effective_date
end_date
I don't think there should be parentheses between BETWEEN and the first term of that expression. Something like this should work:
SELECT amount
FROM Prices
WHERE item_id = 1246 AND
'2016-12-26' BETWEEN effective_date AND COALESCE(end_date, NOW())
This question is a typo, but maybe this answer would be useful to anyone who wants to know the proper way to use BETWEEN.
The MySQL documentation for BETWEEN doesn't explicitly mention anything about parentheses, but it seems to be implying this based on the examples given.
Based on testing this locally, parentheses around each of the two terms in the BETWEEN expression are OK, e.g.
WHERE '2016-12-26' BETWEEN (effective_date) AND (COALESCE(end_date, NOW()))
However, putting parenthesis around the entire clause generates an error, which is what you were doing:
WHERE '2016-12-26' BETWEEN (effective_date AND COALESCE(end_date, NOW()))
This question is all about laziness... I'd like to do something like this:
select some_func(some_col), * from my_table
So that I don't have to do this:
select some_func(some_col), col_1, col_2... col_ad_infinitum from my_table
Is there any way to make the first query work? This is the error I get when I run it:
ERROR 1064 (42000) at line 1: 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 my_table' at line 1
Do you mean that in MySQL your first query:
SELECT some_func(some_col), *
FROM my_table
produces this error?:
Error Code: 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
You can change your code into (this results in no errors!):
SELECT *, some_func(some_col)
FROM my_table
or into this, if you want to have the calculated columns first:
SELECT some_func(some_col), t.*
FROM my_table AS t
Unfortunately, mysql only supports the asterisk at the start of the column list (unlike every other DB I am familiar with)
(Edited: start not end - oops!)
Change the order of your select params:
select *,some_func(some_col) from my_table
Anyway, as the Zen of Python says: "Explicit is better than implicit". Always try to write the fields you're selecting, and if it's posible try to put the table they're from too, you can use an alias. Your future YOU will thank you.
select t.some_col from my_table t
When I do that with PostgreSQL, I get the column(s) I specify followed by all the other columns (possibly repeating the column(s) I specified).