I was trying to get a sum of few selected rows of Money Column in a database.
The sql is below.
$sql_total_money = "SELECT SUM(Money) as TotalMoney FROM accounts WHERE Program='PSC' BranchId='13' and ExamYear='2013'";
$result_total_money=mysql_query($sql_total_money,$link)or die($sql_total_money."<br/><br/>".mysql_error());
$row_total_money=mysql_fetch_array($result_total_money);
This is giving an error like below because of die function.
SELECT SUM(Money) as TotalMoney FROM accounts WHERE Program='PSC' BranchId='10' and ExamYear='2013'
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 'BranchId='10' and ExamYear='2013'' at line 1
What can i do and what is the solution?
You have missed an AND after Program='PSC':
SELECT SUM(Money) as TotalMoney
FROM accounts
WHERE Program='PSC'
AND BranchId='10'
AND ExamYear='2013'
You need to club your condition(s) using conditional operator AND/OR like
SELECT SUM(Money) as TotalMoney
FROM accounts
HAVING Program='PSC' and
BranchId='10' and
ExamYear='2013'
Related
I would like to show total purchase followed by date from purchase table to insert into total_purchase table as in date and buy as well as view
my purchase table structure as below:
id|companyName |purchase|date
1|housedotcom |1300 |2016-1-1
2|homedotcom |1234 |2016-1-1
3|gardendotco |1000 |2016-1-2
4|landotcom |999 |2016-1-2
5|garagedotcom|5400 |2016-1-2
6|homedotcom |2600 |2016-1-2
7|housedotcom |1900 |2016-1-2
my total_purchase table as below
id|date |buy
1|2016-1-1|2534
2|2016-2-2|20890
I tried this into sql
INSERT INTO 'total_purchase'(date,buy) SELECT date, sum(purchase)
*FROM 'purchase' GROUP BY date;
And it showed in mysql result as I expected but when I tried insert new data into purchase table as same companyName with different date in mysql it says duplicate as well as in php coding sql it did not worked and showed this error
Error Processing RequestSQLSTATE[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 'FROM purchaseGROUP BY date'
Any suggestions? Thanks in advance.
remove * from your query to be:
INSERT INTO total_purchase (date,buy) SELECT date, sum(purchase)
FROM purchase GROUP BY date;
For inserting value should be entered by just column by column.In Select you use * that return row more than one remove '*'
INSERT INTO 'total_purchase'(date,buy) SELECT date, sum(purchase)
FROM 'purchase' GROUP BY date;
I am relatively new to somewhat advanced MySQL querying. I had been trying to query the most recent order in an order table of a particular user using MySQL SELECT statement using the following MySQL query.
SELECT o1.* FROM order AS o1
WHERE o1.orderDateTime =
(
SELECT MAX(o2.orderDateTime) FROM order AS o2
WHERE o2.userId = '1'
)
But I had been constantly getting the following MySQL error #1064 related to MySQL syntax.
#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 'order AS o1 WHERE o1.orderDateTime = (SELECT MAX(o2.orderDateTime)FROM order AS ' at line 1
I got similar errors in relation with INSERT statements but I managed to fix it up using the methods specified in MySQL 1064: You have an error in your SQL syntax
I made every effort to fix the query in the current case but I was still unsuccessful.
I would be grateful to you if someone can help me out with fixing this MySQL syntax error for SELECT clause specified above. It would be great if someone could specify me the exact reason for the occurrence of this issue, as well.
order is a reserved word and its a bad choice for table name. You need to escape using backticks in the query
SELECT o1.* FROM `order` AS o1
WHERE o1.orderDateTime = (
SELECT MAX(o2.orderDateTime) FROM `order` AS o2
WHERE o2.userId = '1'
)
http://dev.mysql.com/doc/mysqld-version-reference/en/mysqld-version-reference-reservedwords-5-5.html
As per #Abhik, order is a MySQL keyword.
And you should avoid collapse with two methods:
Use backticks (`) (#Abhik has already explained this.)
Prepend Database name before Table Name e.g.
DataBase_Name.order.
But, still #Abhik's approach is preferable as in case of database name change, you need to change DataBase name in your query.
First of all you could follow #Abhik Chakraborty suggestion to include back ticks around order table name. order is a reserved word in mysql. My suggestion was to improve your sql query. YOu could acomplish the same using:
SELECT o1.* FROM `order` o1
WHERE o1.userId = '1' order by orderDateTime desc limit 1
the subquery seems unnecessary.
I have a non-working query as the sub-query FROM clause doesn't understand CONCAT():
SELECT *
FROM `events` e
WHERE EXISTS (SELECT *
FROM CONCAT('prefix_', e.`event_id`) registrations
WHERE registrations.`attendee` = 123456
)
Is there any way to make this work in a single-statement?
The error message I receive is:
#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 '('prefix_', e.event_id) registrations
You are trying to dynamically come up with a table name? I don't think this is possible...because doesn't the FROM clause table name need to be resolved prior to the SQL being able to be evaluated and executed?
SELECT * FROM (SELECT *,
CONCAT('prefix_', `event_id`) as prefix FROM `events`
WHERE events.attendee = 123456
);
This line of code
$SQL = "SELECT * FROM stats ORDER BY Team WHERE Team='$teamval'";
is returning with the following MySQL 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 'WHERE Team='OTT''
at line 1
I can't find anything wrong with the syntax, what's wrong with it?
The ORDER BY clause must appear after the WHERE clause. So, your query should, instead, be:
SELECT * FROM stats WHERE Team='$teamval' ORDER BY Team
You have used in correct syntax of using order by before where clause , ORDER BY should be used at the end of query if you have used limit in your query then put order by before limit
SELECT * FROM stats WHERE Team='$teamval' ORDER BY Team
Replace:
$SQL="SELECT * FROM stats WHERE Team='$teamval' ORDER BY Team";
I have 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 'select manufacturers_id, manufacturers_name, manufacturers_image, date_added, la' at line 1
The query:
SELECT COUNT(
SELECT manufacturers_id,
manufacturers_name,
manufacturers_image,
date_added,
last_modified
FROM manufacturers
ORDER BY manufacturers_name
) AS total
This happens also to some other categories in my website. All finish their line incomplete.
This query would do what you want:
SELECT COUNT(*) AS total
FROM manufacturers
What's wrong with your query:
COUNT() accepts an expression or single column as parameter - you're passing multiple
ORDER BY doesn't make much sense as soon as what you need is just number of rows (for obvious reason that number doesn't depend on order)
Maybe because the code is not on here full but what I can see is the mysql/mysqli_query is missing, and the ";" at the end of the code.