Mysql syntax error most popular - php

I am trying to retrieve the top 5 values in a certain column in a mysql database.
I have the following query:
SELECT `dep_aerodrome`,
Count(`dep_aerodrome`) AS `cnt`
FROM sectors
WHERE ( `group` = '".$_SESSION['GROUP']."'
AND ( $bases ) NOT IN dep_aerodrome )
GROUP BY `dep_aerodrome`
ORDER BY `cnt` DESC
LIMIT 5
The query contains a WHERE clause and NOT IN to not include airports that are registered as bases.
I am getting the following syntax 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 'dep_aerodrome) GROUP BY dep_aerodrome ORDER BY cnt DESC LIMIT 5' at line 1
and I cannot figure out where it is coming from. Can anyone help out?

Try this:
SELECT `dep_aerodrome`,
Count(`dep_aerodrome`) AS `cnt`
FROM sectors
WHERE `group` = '".$_SESSION['GROUP']."'
AND ( $bases )
AND dep_aerodrome NOT IN ($some_values)
GROUP BY `dep_aerodrome`
ORDER BY `cnt` DESC
LIMIT 5
I think you want to do this like this. $some_values can be in the format of 'value1','value2',... as in:
dep_aerodrome NOT IN ('value1','value2',...)

The right syntax for using NOT IN is
WHERE column_name NOT IN ('value1','value2',...)
And If you want to match 1 value with multiple columns check this.
Different approach of using IN clause in MySql

Related

How can I get total documents/objects retrieved from an MySQL query?

I have this query: SELECT SQL_CALC_FOUND_ROWS DISTINCT something.* FROM someting WHERE TRUE AND something.is_active=1 ORDER BY long_id DESC. The query above will retrieve some objects and document.
Is it possible to get the total number of documents/objects from that query before retrieving the documents/objects themselves?
The case here is that table "something" can have over 100,000 documents/objetcs hence cannot be handled by PHP (returning fatal error insufficient memory). So I want to know how many documents/objects will be retrieved from the query.
I have tried any combinations of COUNT:
SELECT COUNT(*) SQL_CALC_FOUND_ROWS DISTINCT something.* FROM something WHERE TRUE AND something.is_active=1 ORDER BY long_id DESC
SELECT COUNT(SQL_CALC_FOUND_ROWS DISTINCT something.*) FROM something WHERE TRUE AND something.is_active=1 ORDER BY long_id DESC
SELECT SQL_CALC_FOUND_ROWS DISTINCT COUNT(something.*) FROM something WHERE TRUE AND something.is_active=1 ORDER BY long_id DESC
but always have 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 ....
SELECT COUNT(DISTINCT something.*) FROM something WHERE something.is_active=1 ORDER BY long_id DESC
Since you use COUNT, there is no need to use SQL_CALC_FOUND_ROWS. Also, I remove TRUE in where clause because it is unnecessary.
SELECT COUNT(*) FROM something WHERE something.is_active=1
Just simplify your query and count all of the results. DISTINCT something.* makes no sense either (unless your table doesn’t have a primary key, which I doubt is the case).

Wrong syntax (MySQL, PHP)

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";

MySqli and PHP: Not in statement

I'm trying to get data by an mysqli query.
Query looks like:
SELECT * FROM pxldr_drawings
ORDER BY RAND()
WHERE id NOT IN (1,3,4,2)
LIMIT 1
But i get the following error message
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 id NOT IN (1,3,4,2) LIMIT 1' at line 3
I also tried NOT IN ('1','3','4','2') and NOT IN(1,3,4,2), but neither worked.
Thanks, LB
WHERE clause must be before ORDER BY clause.
try this
SELECT * FROM pxldr_drawings
WHERE id NOT IN (1,3,4,2)
ORDER BY RAND()
LIMIT 1
The order of clauses is important, order by should come after where.
SELECT * FROM pxldr_drawings
WHERE id NOT IN (1,3,4,2)
ORDER BY RAND()
LIMIT 1
For the proper syntax of where different clauses need to be placed, please refer to "Select Syntax" documentation.

SQL/PHP query error

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.

Error when using SQL_CALC_FOUND_ROWS()

I'm doing a simple MySQL query to count the number of rows a query is returning without the effect of the LIMIT clause. I'm using Active Records with Codeigniter PHP framework.
Problem: I'm getting an error when SQL_CALC_FOUND_ROWS is used. Why is this so?
Query
SELECT `listing_id`, SQL_CALC_FOUND_ROWS listing_id FROM (`listings`) LIMIT 100
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 'SQL_CALC_FOUND_ROWS listing_id FROM (listings) LIMIT 100' at
line 1
Codeigniter Active Records
$this->db->select('listing_id')
->select('SQL_CALC_FOUND_ROWS listing_id', FALSE)
->from('listings')
->where('price < 1000')
->limit($limit, $offset)
->order_by('listing_id', 'desc');
SQL_CALC_FOUND_ROWS doesn't return a value, it's simply a modifier to indicate that the number of rows —where the LIMIT clause is not taken into account— should be saved so it can be retrieved later on, by using a second query (without generating the complete result set twice). Think of it the same as the DISTINCT keyword.
For more information, please read the documentation on this topic.
You can use MySql's count() function to achieve what you are looking for ie.
SELECT `listing_id`, count(*) FROM (`listings`) LIMIT 100

Categories