Not Able to Count Less/ Greater Than a Value in MySQL Table - mysql

I am trying to COUNT rows less or greater than 100 in MYsQL table using PHPMyAdmin
SELECT HAVING COUNT(`roadLength`) > 100 AS stop3 FROM `single-ecolo-dis-yes-tbl`
and
SELECT HAVING COUNT(`roadLength`)< 100 FROM `single-ecolo-dis-yes-tbl`
but I am getting this error
SELECT HAVING COUNT(`roadLength`) > 100 FROM `single-ecolo-dis-yes-tbl`
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 'HAVING COUNT(`roadLength`) > 100 FROM `single-ecolo-dis-yes-tbl`
LIMIT 0, 25' at line 1
can you please let me know why this is happening? and how I can fix this?

having is to reduce the groups in a query and has no business in the select clause. It is a clause for itself
SELECT SUM(`roadLength` > 100) AS stop3
FROM `single-ecolo-dis-yes-tbl`

Normally, for a simple count like this, you would put the condition in the WHERE clause:
SELECT COUNT(*) as stop3
FROM `single-ecolo-dis-yes-tbl` t
WHERE t.roadLength > 100 ;
This allows the optimizer to use an index on roadLength, if one is available.

Related

LIMIT in MySQL Update Row

I am trying to Update Some row in my database. If I run without limit its working fine but if I run it with limit its giving me error like below
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 '35' at line 1
My Query is like below
UPDATE number_list SET sync = 0 WHERE server = 1 ORDER by id ASC LIMIT 0,35
Let me know if someone can correct me.
You can use limit in an update (in MySQL) but not an offset. So just do:
UPDATE number_list
SET sync = 0
WHERE server = 1
ORDER by id ASC
LIMIT 35;
This is a bit subtle, because SELECT supports offsets. However, it is clear in the syntax diagram for UPDATE.

MySQL phpmyadmin - SELECT FOR UPDATE not recognised

I'm writing a PHP program and wanna to implement row-level locking to avoid concurrent user update/delete for the same record.
But I hit error "Unrecognised keyword" when using SELECT FOR UPDATE. Table type is innoDB.
Am i missing any setup for my database?
SELECT * FROM companyTable
WHERE companyId = "0000001"
FOR UPDATE;
Error
Static analysis:
1 errors were found during analysis.
Unrecognized keyword. (near "FOR" at position 57)
SQL query: Documentation
SELECT * FROM companyTable WHERE companyId = "0000001" FOR LIMIT 0, 30
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 'LIMIT 0, 30' at line 3
There Is an Syntax Error Try this:
Its Select All Records From 1 - 30
SELECT * FROM companyTable WHERE companyId = "0000001" ORDER BY id LIMIT 30;
First problem seems to be, that in your SQL query is missing keyword UPDATE
SELECT * FROM companyTable WHERE companyId = "0000001" FOR LIMIT 0, 30
And the second issue can be, that syntax SELECT ... FOR UPDATE in MySQL doesn't support LIMIT.
So your SQL query should be:
SELECT * FROM companyTable WHERE companyId = "0000001" FOR UPDATE
In phpmyadmin, it is difficult to remove auto added LIMIT clause - try another MySQL client.
PhpMyAdmin is not build to handle(Test) transaction control, use Mysql Console or php session instead
and use "begin" to start the Transaction before the code
begin;
select * from `tblx` where `idx`=1 for update;
update `tblx` set `field`='xx' where `idx`=1;
commit;

SQL Query in XAMPP

SELECT * FROM customers WHERE Age > ALL (SELECT Age FROM customers WHERE Salary > 6800);
Actually I am trying to learn the usage of ALL in SQL. But unfortunately when I am trying to execute this query it's giving me the error in XAMPP.
Static analysis:
2 errors were found during analysis.
Unrecognized keyword. (near "ALL" at position 36) Unexpected token.
(near "(" at position 40) SQL query: Documentation
SELECT * FROM customers WHERE Age > ALL ( LIMIT 0, 25
MySQL said: Documentation
1064 - 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 'LIMIT 0, 25' at line 1
I would like to know what is the exact syntactical error in my query.
Here's my Table:
Customer Table
There is nothing syntactically wrong with your query, you have an unfortunate version of phpMyAdmin which messes it up. The problem was present in 4.5.1, not present in 4.6.4, it was fixed some time in between.
This is the query that you send (OK):
SELECT * FROM customers WHERE Age > ALL (SELECT Age FROM customers WHERE Salary > 6800);
This is the query that server receives from phpMyAdmin (very wrong):
SELECT * FROM customers WHERE Age > ALL ( LIMIT 0, 25
Hence the problem.

Querying SQL Set

I have an SQL field defined as set('nightlife', 'food', 'sports', 'culture', 'movies', 'general')
Now I want to run a query where I pass for example nightlife,food and I want the result to contain ALL records where the Category contains nightlife or food. So for example a record with nightlife,culture,sports should also be returned as it contains nightlife. I'm not sure how to run this. I tried using the IN keyword in the following way:
'SELECT ... FROM table WHERE '$category' IN Categories
however this isn't working.
UPDATE
Running following query as suggested in answer:
SELECT *
FROM images
WHERE id = '2650225'
AND WHERE FIND_IN_SET('sports', Categories ) >0
OR FIND_IN_SET('nightlife', Categories ) >0
ORDER BY delete_at ASC
LIMIT 10
OFFSET 0
receiving 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 'WHERE find_in_set('sports', Categories) > 0 or find_in_set('nightlife', Categori' at line 1
You can use find_in_set with or:
select *
from yourtable
where find_in_set('nightlife', categories) > 0 or
find_in_set('food', categories) > 0
SQL Fiddle Demo
Based on your edits, you can't have multiple where clauses. Also you need to use parentheses around the or criteria:
SELECT *
FROM images
WHERE id = '2650225' AND
(FIND_IN_SET('sports', Categories ) > 0 OR
FIND_IN_SET('nightlife', Categories ) >0)
ORDER BY delete_at ASC
LIMIT 10
OFFSET 0

DELETE FROM `table` AS `alias` ... WHERE `alias`.`column` ... why syntax error?

I tried this with MySQL:
DELETE FROM `contact_hostcommands_relation` AS `ContactHostCommand` WHERE (`ContactHostCommand`.`chr_id` = 999999) LIMIT 1
And I get this:
#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 (`ContactHostCommand`.`chr_id` = 999999) LIMIT 1' at line 1
Note: This query is automatically generated and conditions are based on table aliases.
Why I get this error?
Is there any way to use table aliases in where clause?
Is this MySQL specific?
What #Matus and #CeesTimmerman said about MSSQL, works in MySQL 5.1.73 too:
delete <alias> from <table> <alias> where <alias>.<field>...
You can use SQL like this:
DELETE FROM ContactHostCommand
USING `contact_hostcommands_relation` AS ContactHostCommand
WHERE (ContactHostCommand.`chr_id` = 999999)
LIMIT 1
You cannot use AS in a DELETE clause with MySQL :
DELETE FROM `contact_hostcommands_relation` WHERE (`chr_id` = 999999) LIMIT 1