MYSQL Incorrect usage of UPDATE and ORDER BY RAND - mysql

I am trying to update with JOIN and RAND
UPDATE table1 a INNER JOIN table2 b USING(id)
SET a.description = CONCAT('different words', b.name)
WHERE a.description LIKE ''
ORDER BY RAND() LIMIT 100;
I get the error : Incorrect usage of UPDATE and ORDER BY
without JOIN the following code works perfectly
UPDATE table1 SET description = CONCAT('different words', name)
WHERE description LIKE ''
ORDER BY RAND() LIMIT 100;
thanks for helping

Try this:
UPDATE a
SET a.description = CONCAT('different words', b.name)
FROM table1 a
INNER JOIN table2 b ON a.id=b.id
WHERE a.description LIKE ''
ORDER BY RAND() LIMIT 100;

Assuming you're using mysql, you should wrap your query that's working (with the user variable and the order by) as an inline view (MySQL calls it a derived table). Reference that whole view as a row source in the UPDATE statement. So your query should be something like this:
UPDATE table1 a
INNER JOIN (
--query
) as someName
SET a.description = CONCAT('different words', b.name)

Related

Sort MySql query by field in exists sub query

I am looking for a way to sort a query by a field that exists within a where exists query.
An attempt to use a field from the exists subquery I get the 'Unknown column' error.
Example query;
select
*
from
`table_a`
where
`a_field` = 'foo'
and exists (
select
*
from
`table_b`
where
`table_a`.`an_id` = `table_b`.`another_id` and `table_b`.`another_field` = 'bar'
)
order by
`table_a`.`created_at` asc,
`table_b`.`another_id` desc;
Is the only solution to use an inner join query like;
select
`t1`.*
from
`table_a` as `t1`
inner join `table_2` as `t2` on `t2`.`another_id` = `t1`.`an_id`
where
`t1`.`a_field` = 'foo'
and `t2`.`another_field` = 'bar'
order by
`t1`.`created_at` asc,
`t2`.`another_id` desc;
Your example query is ordering by another_id, which is used in the correlation clause. So, you can just do:
select a.*
from table_a a
where a.a_field = 'foo' and
exists (select 1
from table_b b
where a.an_id = b.another_id and
b.another_field = 'bar'
)
order by a.created_at asc,
a.an_id desc;
Assuming you actually want a different column, you can use a JOIN. The issue is that more than one row may match. So you need to remove the duplicates in the subquery:
select a.*
from table_a a join
(select b.another_id, max(b.another_col) as another_col
from table_b b
where another_field = 'bar'
group by b.another_id
) b
on a.an_id = b.another_id
where a.a_field = 'foo'
order by a.created_at asc, b.another_id desc;
You can only use your form of the JOIN if you know that at most one row will match.

MySQL Update using INNER JOIN with ORDER BY and LIMIT

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'

Why do I receive an error at this join tables?

I wrote this:
SELECT DISTINCT CATEGORY FROM T AS T1
CROSS JOIN (SELECT *
FROM T
WHERE T.CATEGORY = T1.CATEGORY
ORDER BY CATEGORY DESC
LIMIT 10)
and I receive this
"Unknown column 'T1.CATEGORY' in 'where clause'".
Why?
Update:
My purpose of this is to get 10 posts of any category.
Because T1 is not visible from within the subquery.
Your JOIN also serves no purpose and/or you probably forgot the JOIN condition.
In JOIN condition should use ON keyword
SELECT DISTINCT CATEGORY FROM T AS T1
CROSS JOIN SELECT * FROM T ON T.CATEGORY = T1.CATEGORY
ORDER BY CATEGORY DESC LIMIT 10;
If you need to get 10 posts of each category you can use a query like this:
SELECT CATEGORY, Post
FROM (
SELECT a.CATEGORY, a.Post, count(*) as rn
FROM #T a
JOIN #T b ON a.CATEGORY = b.CATEGORY AND a.Post >= b.Post
GROUP BY a.CATEGORY, a.Post) dt
WHERE rn < 11;

MySQL reference outer table alias in subquery error

select c.*
from posts p
join comments c on (p.id=c.post_id)
and c.status = 'A'
and c.id >= (select MIN(id) from (select id from comments where post_id=p.id order by id DESC LIMIT 7) as c2)
where p.comments_count > 0 AND p.id IN (247,245,244,243,242,241)
In the above sql query I get this mysql error Unknown column 'p.id' in 'where clause' it seems like in the subquery p.id isn't able to get referenced. Is there anyway that I can get the posts table id referenced in this subquery?
I think you are trying to fetch latest 7 comments for each post. Could you try this? you can test here http://www.sqlfiddle.com/#!2/a222e/3/0
The First Attempt
I tried below SQL.
SELECT *
FROM comments t1
WHERE post_id IN (247,254,244,243,242,241)
AND id IN (
SELECT id
FROM comments
WHERE t1.id = id
LIMIT 7
);
But I got an error "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery"
Another Approach
So, I tried self join on comments to generate sequence number.
SELECT id
FROM (
SELECT t1.id, COUNT(*) AS cnt
FROM comments t1 INNER JOIN comments t2
ON t1.post_id = t2.post_id
WHERE t1.id <= t2.id
AND t1.post_id IN (247,254,244,243,242,241)
AND t2.post_id IN (247,254,244,243,242,241)
GROUP BY t1.id
) x
WHERE cnt <= 7;
inner sub-query uses self join and produces cnt column which has sequential value for each comment id of post.
But preceding query only fetches id of comment
Finally to get all columns of comment table, following query should be executed.
SELECT *
FROM comments c INNER JOIN (
SELECT id
FROM (
SELECT t1.id, COUNT(*) AS cnt
FROM comments t1 INNER JOIN comments t2
ON t1.post_id = t2.post_id
WHERE t1.id <= t2.id
AND t1.post_id IN (247,254,244,243,242,241)
AND t2.post_id IN (247,254,244,243,242,241)
GROUP BY t1.id
) x
WHERE cnt <= 7
) t USING (id);
Using User Variables
Actually you have another chance using MySQL user variable. I didn't mention this interesting MySQL feature because I was not sure I understood your question correctly.
SELECT *
FROM (
SELECT post_id, id,
IF (#pid = post_id, #cnt := #cnt + 1, #cnt := 1) AS cnt,
#pid := post_id
FROM comments, (SELECT #pid := 0, #cnt := 0) tmp
WHERE post_id IN (247,254,244,243,242,241)
ORDER BY post_id, id DESC
) x
WHERE cnt <= 7;
Preceding SQL looks like simpler (means good performance) than older join version. but not tested on large data set.
You can only access the outer query 1 level deep. So try doing you where filter on the middle query:
SELECT MIN(id)
FROM (
SELECT id, post_id
FROM comments
ORDER BY id DESC
LIMIT 7
)
WHERE post_id = p.id

MySQL Query Optimisation - Single query restriction

With my restricted access, I can run only a single query at a time. Quite often I have to run a query similar to the one below for tens of IDs. One by one it turns to be a pretty time-consuming task.
UPDATE table1
SET columnA = X,
columnB = (SELECT max(columnC) FROM table2 WHERE id = <ID>)
WHERE id = <ID>
Ideally I would like to use something like id in {id1, id2, ...}, however both instances of ID must be the same.
I am trying to modify the query so it can process number of ID in a single query.
==================================================
SOLUTION (quite simple actually):
UPDATE table1
SET columnA = X,
columnB = (SELECT max(columnC) FROM table2 WHERE id = table1.id)
WHERE id in (id1, id2, ...)
Try belwo. Please do required changes because I have explained just way to do
UPDATE table1 as t1 INNER
JOIN (
SELECT id
, Max(columnC) As maxval
FROM table12
GROUP
BY id
) As t2 ON t1.id= t2.id
SET columnA = t1.x
FROM columnB = t2.maxval
WHERE id = <ID>
you might want to try this.
for T-SQL:
UPDATE a
SET a.columnA = X,
a.columnB = max(b.columnC)
FROM table1 a INNER JOIN table b ON a.id = b.id
WHERE a.id IN ('ID','HERE',...)
UPDATE
for MySQL:
UPDATE table1 a INNER JOIN
(SELECT id, Max(columnC) As iMax
FROM table12
GROUP BY id) b ON a.id = b.id
SET a.columnA = X,
a.columnB = b.iMax
WHERE a.id IN ('ID','HERE',...)
REFERENCE