Drop the ORDER BY + LIMIT, or the JOIN, and everything is peaches. Put them together and I seem to release the Kraken. Anyone who can shed some light?
DELETE table1 AS t1
FROM table1 AS t1 LEFT JOIN table2 AS t2 ON t1.id = t2.id
WHERE t2.field = 'something'
ORDER BY t1.id DESC
LIMIT 5
(Delete using aliases)
I've also tried it without aliases & dropping the WHERE, to no avail. Always a syntax error "near 'ORDER BY...".
From Mysql Docs: DELETE
For the multiple-table syntax, DELETE deletes from each tbl_name the rows that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.
In your case, I think this works:
DELETE
FROM table1
WHERE EXISTS
( SELECT t2.id
FROM table2 AS t2
WHERE t2.id = table1.id
AND t2.field = 'something'
)
ORDER BY id DESC
LIMIT 5
If you really need to do this you can do the following
DELETE table1
WHERE id in
(SELECT t.id
FROM table1 AS t INNER JOIN table2 AS t2 ON t1.id = t2.id
WHERE t2.field = 'something' --No point in doing a LEFT JOIN because of this
ORDER BY t1.id DESC
LIMIT 5)
t1 was not declared as an alias. Try t everywhere you have t1 (or vice versa).
Related
I have 2 tables, one is table1
and another is table 2
I want the result by a query, like
I have tried select id from table2 order by (select id from table1); but it is giving error.
You can join and sort. But you need a column that defines the ordering of the rows in table1. Let me assume that you have such column, and that is is called ordering_id.
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id
order by t1.ordering_id
You can even use a subquery in the order by clause:
select *
from table2 t2
order by (select t1.ordering_id from table1 t1 where t1.id = t2.id)
Join the two tables and then order the result.But for that you need to have some column for ordering and this does not seems to be the case. Syntax you are using for ordering will not work.
SELECT A.ID, B.NAME FROM TABLE1 A INNER JOIN TABLE2 B
ON(A.ID = B.ID) ORDER BY A.ID DESC
finally got the answer
select t2.*
from table2 t2
inner join table1 t1 on t1.id = t2.id;
Update table2 t2
set t2.c1=t1.c1
inner join table1 t1 on ( t1.c2 = t2.c2)
where t2.c1 = "-1";
I want to execute the above query which will update the table2 column from table1 column ON INNER JOIN matching conditions. It is working fine. I am running a migration where the rows count are in million in both tables. I thought of limiting the update query in batches for query optimization but limit is not allowed in update query.
I can try with select query with limit option, but updating multiple columns would not work with this below query.
update table2 t2
set t2.c1=<?>
where t1.c2 = ( select c2 from table);
Can anyone help to use update query with optimization? Will updating millions row have any impact?
You could move the limiting clause to the joined table, like so:
update table2 t2
inner join (
select c1, c2
from table1
order by c2
limit ?, ?
) t1 on t1.c2 = t2.c2
set t2.c1 = t1.c1
where t2.c1 = -1
I am not sure what you really want to do, but you can update multiple columns with update and limit.
The following is fine:
update table2 t2
set t2.c1 = <?>,
t2.c3 = ?
where t1.c2 = ( select c2 from table)
limit 100;
I'm trying to JOIN a table with a subquery to limit data to only 1 last row, matching certain values from other FROM or JOINed tables:
SELECT
t1.column1,
t1.column2,
t2.column1,
t3.column2
FROM
table1 t1
JOIN
(
SELECT column1
FROM table2
WHERE t1.column2 > table2.column1
ORDER BY table2.date DESC
LIMIT 1
) t2
JOIN table3 t3 ON t2.column1=t3.column2
Getting an error: Unknown column t1.column2 in 'where clause'. Seems I can't address other columns inside the subquery.
Is what I'm trying to do possible?
If so, what am I doing wrong / what other way could I try this?
In MySQL, implicit and explicit joins together in a single query create the problem. You can not use an implicit JOIN based on a nested SELECT - FROM - WHERE query when there is another explicit JOIN present.
Try below.
SELECT *
FROM table1 t1
INNER JOIN table2 t2 ON t1.column2 > t2.column1
LEFT OUTER JOIN table3 t3 ON t2.column1=t3.column2
You may re-write your query as -
SELECT t1.column1,
t1.column2,
t2.column1,
t3.column2
FROM table1 t1
JOIN (SELECT column1
FROM table2
ORDER BY table2.date DESC
LIMIT 1) t2 ON t1.column2 > t2.column1
JOIN table3 t3 ON t2.column1=t3.column2
The solution for me was:
SELECT
t1.column1,
t1.column2,
t2.column1,
t3.column2
FROM
table1 t1
JOIN table2 t2 ON t2.id =
(
SELECT id
FROM table2
WHERE t1.column2 > table2.column1
ORDER BY table2.date DESC
LIMIT 1
)
JOIN table3 t3 ON t2.column1=t3.column2
More info on:
CROSS/OUTER APPLY in MySQL
https://dba.stackexchange.com/a/170760/13156
Using the following query, I am able to return the results I need.
SELECT t1.*
FROM lms_attendance t1
WHERE t1.id = (SELECT t2.id
FROM lms_attendance t2
WHERE t2.user = t1.user
ORDER BY t2.id DESC
LIMIT 1)
However, when trying to do a DELETE instead, this does not work. Through trial and error, I either get unknown column or can't specify target updated in from clause. The main issue here is that I absolutely have to have the WHERE clause as it is so that it returns the maximum id for each user, and not for the entire table.
Try this query below. Please run a select first to confirm if it is the correct records before running the delete. Better if you keep a backup table too.
Run select:
SELECT t1.* FROM lms_attendance t1
LEFT JOIN (SELECT user, MAX(id) id
FROM lms_attendance
GROUP BY user) t2
ON t1.id = t2.id
WHERE t2.id IS NULL
If looks OK;
DELETE t1.* FROM lms_attendance t1
LEFT JOIN (SELECT user, MAX(id) id
FROM lms_attendance
GROUP BY user) t2
ON t1.id = t2.id
WHERE t2.id IS NULL
I'm trying to do an update using an inner join with limit and order by (although the order by is not essential. From what I have read up the standard update will not work... this is what I am trying to do:
UPDATE table1
INNER JOIN table2
ON table1.service_id=table2.service_id
SET table1.flags = NULL
WHERE table1.type = 'fttc'
AND table1.flags = 'co'
AND table2.sync not like '%Yes%'
AND table1.date >= $today_date
ORDER BY table1.priority ASC
LIMIT 20;
it is for use in a case management tool and using php, I want to update 20 tickets i.e. remove the 'flag' so that they can be worked, the quantity will be passed as a variable, so I want to update 20 tickets for example highest 'priority' first, if that can be done?
If I read your question correctly, you want to perform an update on the first 20 records which results from the join, using the priority as ordering. You cannot do this directly in an UPDATE in MySQL AFAIK, but you can create an updatable view and then update that.
CREATE VIEW yourView
AS
SELECT
t1.service_id,
t2.service_id,
t1.flags,
t1.type,
t1.date,
t1.priority,
t2.sync
FROM table1 t1
INNER JOIN table2 t2
ON t1.service_id = t2.service_id
WHERE t1.type = 'fttc' AND
t1.flags = 'co' AND
t2.sync NOT LIKE '%Yes%' AND
t1.date >= $today_date
ORDER BY t1.priority
LIMIT 20;
And then update this view:
UPDATE yourView
SET flags = NULL
There should be no reason to use a view:
UPDATE table1 t1
SET t1.flags = NULL
WHERE t1.type = 'fttc' AND
t1.flags = 'co' AND
t1.date >= $today_date AND
EXISTS (SELECT 1
FROM table2 t2
WHERE t2.service_id = t1.service_id AND
t2.sync not like '%Yes%'
)
ORDER BY t1.priority ASC
LIMIT 20;
You cannot use ORDER BY and LIMIT with a multiple table JOIN. However, you can move the condition on table2 to the WHERE clause.
Following work for me:
UPDATE child AS upd
JOIN (SELECT t1.id FROM child AS t1
INNER JOIN master AS t2
ON t2.id = t1.id
where 1
AND t2.`date` BETWEEN '2020-06-23 00:00:00' AND '2020-06-23 23:59:59'
AND t2.client_id= 10 AND t1.code NOT IN('11','22')
order by t1.id desc LIMIT 1) AS col
ON upd.id=col.id
SET upd.code= '33', upd.`resp` = 'done',upd.status='success'