Delete only one record in mysql database - mysql

what is the proper way to delete only one record from mysql data base.
this query used for select only one record.
SELECT * FROM Customers WHERE Country='Mexico' ORDER BY Country ASC LIMIT 1;
the above query run correctly .but when replace select to delete that not worked.
DELETE FROM Customers WHERE Country='Mexico' ORDER BY Country ASC LIMIT 1;
how I can fix it?

If you have an id column you can use a subselect. I have removed the order by since this will be the same like order by 'Mexico' asc which is pretty useless.
DELETE FROM Customers
WHERE (CustomerID IN ( SELECT CustomerID
FROM Customers where country = 'Mexico'
ORDER BY CustomerID ASC LIMIT 1 )) ;

I think below query will help you. You will need to have some key ID to differentiate.
DELETE FROM Customers
WHERE SOME_KEY_ID IN
(
SELECT SOME_RANDOM_ID FROM
(
SELECT B.SOME_KEY_ID SOME_RANDOM_ID FROM Customers as B
where Country = 'Mexico'
LIMIT 1
) as c
) ;
Note: The inner select SOME_RANDOM_ID is required, else sqlfiddle throws errors This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery': .
Reference FIDDLE Here

Related

SELECT ID FROM subquery with limit 1000 rows in MySQL

I want to select 1000 rows from another table using the last 1000 ID from another table. This is the query but it returned an error message. What did I do wrong with this query?
SELECT * FROM table1
WHERE id IN
(
SELECT id FROM table2
LIMIT 50
)
Error message received.
Error Code : 1235
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
SELECT *
FROM table1 x
JOIN
( SELECT id FROM table2 ORDER BY id LIMIT 50 ) y
ON y.id = x.id;
You should join both tables and sort in descending order and then apply the limit.
SELECT table1.* FROM table1
JOIN table2 USING (id)
ORDER BY id DESC
LIMIT 1000;
This will give you 1000 entries with highest idin descending order if, and only if, they exist in both tables.
It is easier to do it like this:
SELECT * FROM TBL1, TBL2
WHERE TBL2.FK_ID = TBL1.ID
ORDER BY TBL2.ID
ASC LIMIT 0,1000
you can only do it if the second table has table1's id as a foreign key, but still has its own id identifying the order of when they are created.
Not sure about how to use the limit though.
I hope it helps.

Check if no results with mysql

I would like to know if there is a way in mysql to check if the query produces some results and if no do execute query. Example:
(SELECT * FROM table WHERE id=2) IF NO RESULT (SELECT * FROM table WHERE id=4)
EDIT
I need only one query, basically first I check if there a result with a param (ex status=0) if there is no result I would like to execute the same query with the param changed to status=2.
Hope it can help
MORE EDIT
Basically I have a table with operatorators and departments and another one with all the users, first I check if there is an available operator in the first table and it's not on holiday, if there is no result I will lselect and admin from the second table, but only if there is no operator availbable
MORE MORE EDIT
This query check if there is an operator available but it doesn't select the admin
query = "SELECT b.id
FROM ".$SupportUserTable." b
INNER JOIN ".$SupportUserPerDepaTable." a
ON b.id=a.user_id
WHERE a.department_id=? AND b.holiday='0' AND a.user_id!=".$_SESSION['id']."
ORDER BY b.assigned_tickets,b.solved_tickets ASC LIMIT 1";
Lastest Solution
This is not exactly what I was looking for, but it works, I'm open to improvments to avoid the execution of two queries:
$query = "SELECT *
FROM(
(SELECT b.id
FROM ".$SupportUserTable." b
INNER JOIN ".$SupportUserPerDepaTable." a
ON b.id=a.user_id
WHERE a.department_id=? AND b.holiday='0' AND a.user_id!=".$_SESSION['id']."
ORDER BY b.assigned_tickets,b.solved_tickets ASC LIMIT 1)
UNION
(SELECT id
FROM ".$SupportUserTable."
WHERE status='2' AND id!=".$_SESSION['id']."
ORDER BY assigned_tickets,solved_tickets ASC LIMIT 1)
) tab
LIMIT 1
";
SELECT * FROM table WHERE id = (
SELECT id FROM table WHERE id IN (2,4) ORDER BY id LIMIT 1
)
You can do like this
Select IF(
(SELECT count(*) FROM table WHERE id=2), (SELECT * FROM table WHERE id=2),(SELECT * FROM table WHERE id=4))
select t.*
from
(SELECT * FROM table
WHERE id in (2,4)
LIMIT 1)t
order by t.id

Mysql Subquery Syntax

I'm wondering why statements like select * as t appear in mysql subqueries like the following.
The following deletes the oldest 3 rows in a table according to a created_time column.
Why is this right
DELETE FROM mytable WHERE id = ANY
( SELECT * FROM ( SELECT id FROM mytable ORDER BY created_time ASC LIMIT 3')as t)
and not
DELETE FROM mytable WHERE id = ANY
(SELECT id FROM mytable ORDER BY created_time ASC LIMIT 3)
?
To me, the second form makes sense. It doesn't work and I'd like to understand why the first is necessary. Specifically, what is t and what does as t do?
In many databases, a subquery in a from clause needs to have an explicit alias. The as is optional. I typically use as for columns and leave it out for tables:
DELETE FROM mytable
WHERE id = ANY ( SELECT * FROM ( SELECT id FROM mytable ORDER BY created_time ASC LIMIT 3') t)
Why you need the subquery is a vagary of MySQL. It doesn't allow the table reference in a delete or update to appear in a subquery clause. Oh, it does allow it in a subquery-within-a-subquery clause. So, it is pretty easy to work around this limitation.

deleting variables by specifying id value with OFFSET

I want to delete specific variables based on 'id' value. but the code below is displaying syntax error near: OFFSET 1. I use a similar code where I use SELECT instead of DELETE and it works fine, What am doing wrong here? Thanks
DELETE FROM users WHERE name = '$name' ORDER BY id ASC LIMIT 1 OFFSET 1
The offset component in LIMIT is not available in MySQL DELETE statements but it is allowed in SELECT statements.
So what you can do to get around this fact, is you can actually join a subselect in a DELETE operation, which will then give you your desired results:
DELETE a FROM users a
INNER JOIN
(
SELECT id
FROM users
WHERE name = '$name'
ORDER BY id
LIMIT 1,1
) b ON a.id = b.id
You cannot specify offset in DELETE's LIMIT clause.
Simple use:
DELETE FROM users WHERE name = '$name' ORDER BY id ASC LIMIT 1;
Try
DELETE FROM users WHERE name = '$name' ORDER BY id ASC LIMIT 1,1
You can not have OFFSET in DELETE query.

JOIN in combination with ORDER BY

I want to find the earliest date in a table. How do I put this into a join statement:
SELECT date FROM table1 WHERE orderno = 222 ORDER BY date LIMIT 1
Orderno 222 can have 1-* many rows in table1 that's why I'm using LIMIT 1
I have table these tables:
Order
OrderLine
ProductionDate
Order can have 1-* ProductionDates so when i do my join of ProductionDate i want to find the earliest date.
So my guess of a sql statement would be something like:
SELECT * FROM Order
LEFT JOIN (
IN SELECT date FROM ProductionDate ORDER BY date ASC LIMIT 1)
but this doesn't work. And i would like to know what i should change to get it to work?
I'm not sure if MySQL supports a LIMIT In a derived table, but if it does, the syntax should be:
SELECT ord.*
FROM `Order` ord
LEFT JOIN (SELECT date FROM ProductionDate ORDER BY date ASC LIMIT 1) t
ON t.date = ord.date
Note that order is a reserved word, so you will need to quote it properly. Either using MySQL dreaded backticks (see above) or - if you configured your server to be standard's compliant - using double quotest like this: "order"
SELECT `date`
FROM `table1`
INNER JOIN `table1`.`date` ON `table2`.`date`
ORDER BY date ASC LIMIT 1
Is that what you mean?