update query with subselects from the same table - mysql

I'm trying to create a query to update the rank field of all the records in the table, based on values from the same table.
I managed to get a working SELECT query that calculates the rank, but I'm having a hard time converting it to an UPDATE. This is the query:
SELECT
((views_count + comments_count) * (172800 / elapsed)) AS rank
FROM (
SELECT
p.views_count,
(UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(p.createdAt)) AS elapsed,
(SELECT COUNT(*) FROM `comments` AS c WHERE c.photo_id = p.id) AS comments_count
FROM `photos` AS p
) AS m
How can I implant it in an UPDATE query, to update each record's rank value?
Thanks! :)
EDIT:
My DB structure:
[Photo]
id
user_id
views_count
rank
createdAt
[Comment]
id
photo_id
content

Just a guess, but something like this should work:
UPDATE `photos`
FROM (
SELECT id
, (views_count + comments_count) * (172800 / elapsed) AS rank
FROM (
SELECT p.id
, p.views_count
, ( UNIX_TIMESTAMP(NOW())
- UNIX_TIMESTAMP(p.createdAt)) AS elapsed
, (SELECT COUNT(*)
FROM `comments` AS c
WHERE c.photo_id = p.id) AS comments_count
FROM `photos` AS p
) AS m
) AS z
SET rank=z.rank
WHERE `photos`.id=z.id

You should write your new ranks to a temporary table and use that to update the original table. try
CREATE TEMPORARY TABLE tmp_tbl AS (
SELECT ((views_count + comments_count) * (172800 / elapsed)) AS rank, m.id
FROM (SELECT
p.views_count,
(UNIX_TIMESTAMP(NOW()) - UNIX_TIMESTAMP(p.createdAt)) AS elapsed,
(SELECT COUNT(*) FROM `comments` AS c WHERE c.photo_id = p.id) AS comments_count
FROM `photos` AS p
) AS m);
and then
UPDATE photos, tmp_tbl SET photos.rank = tmp_tbl.rank WHERE photos.id=tmp_tbl.id;
and finally
DROP TABLE tmp_tbl;
See this simliar stackoverflow question

Related

How can update multiple rows with a single sql query?(with "order by")

I have this user table here:
(I want to update ranking based on score)
Here is SQL Fiddle :
http://sqlfiddle.com/#!9/2f459d/1/0
Also code review :
create table users2 (
ranking int ,
user_id int auto_increment primary key,
user_n varchar(70) charset utf8mb4 not null comment 'username',
score int default '0' null,
constraint username
unique (user_n) );
With values :
INSERT INTO users2 (user_id, user_n, score)
value(1,'mohamad',50),
(2,'john',100),
(3,'nik',150),
(4,'sara',200),
(5,'tom',250);
I want to update ranking based on the score. I can select but I can't update that :
SELECT c.u_rank,a.user_id,a.user_n,score from users2 a LEFT JOIN (SELECT #s:=#s+1 u_rank,user_id
FROM users2 d,(SELECT #s:=0) as b order by d.score DESC ) AS c
ON a.user_id = c.user_id order by score DESC
My efforts so far :
UPDATE users2 a LEFT JOIN (SELECT #s:=#s+1 u_rank,user_id
FROM users2 ,(SELECT #s:=0) as b ORDER BY score DESC ) AS c
ON a.user_id = c.user_id SET a.ranking = c.u_rank
In MySQL, you can do this using variables by setting the variable first and then using ORDER BY in the UPDATE:
SET #s := 0;
UPDATE users2 u
SET u.ranking = (#s := #s + 1)
ORDER BY score DESC ;
Here is a SQL Fiddle.

SQL: How to get cells by 2 last dates from 3 different tables?

I have 3 tables (stars mach the ids from the table before):
product:
prod_id* prod_name prod_a_id prod_b_id prod_user
keywords:
key_id** key_word key_prod* kay_country
data:
id dat_id** dat_date dat_rank_a dat_traffic_a dat_rank_b dat_traffic_b
I want to run a query (in a function that gets a $key_id) that outputs all these columns but only for the last 2 dates(dat_date) from the 'data' table for the key_id inserted - so that for every key_word - I have the two last dat_dates + all the other variables included in my SQL query:
So... This is what I have so far. and I don't know how to get only the MAX vars. I tried using "max(dat_date)" in different ways that didn't work.
SELECT prod_id, prod_name, prod_a_id, prod_b_id, key_id, key_word, kay_country, dat_date, dat_rank_a, dat_rank_b, dat_traffic_a, dat_traffic_b
FROM keywords
INNER JOIN data
ON keywords.key_id = data.dat_id
INNER JOIN prods
ON keywords.key_prod = prods.prod_id
Is there a possability to do this with only one query?
EDIT (FOR IgorM):
public function newnew() {
$query = $this->db->query('WITH CTE AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY dat_id ORDER BY dat_date ASC) AS
RowNo FROM data
)
SELECT *
FROM CTE
INNER JOIN keywords
ON keywords.key_id = CTE.dat_id
INNER JOIN prods
ON keywords.key_prod = prods.prod_id
WHERE RowNo < 3
');
$result = $query->result();
return $result;
}
This is the error on the output:
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 'CTE AS ( SELECT *, ROW_NUMBER() OVER (' at line 1
WITH CTE AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY dat_id ORDER BY dat_date ASC) AS RowNo FROM data ) SELECT * FROM CTE INNER JOIN keywords ON keywords.key_id = CTE.dat_id INNER JOIN prods ON keywords.key_prod = prods.prod_id WHERE RowNo < 3
For SQL
WITH CTE AS
(
SELECT *,
ROW_NUMBER() OVER (PARTITION BY dat_id ORDER BY dat_date ASC) AS
RowNo FROM data
)
SELECT *
FROM CTE
INNER JOIN keywords
ON keywords.key_id = CTE.dat_id
INNER JOIN prods
ON keywords.key_prod = prods.prod_id
WHERE RowNo < 3
For MySQL (not tested)
SET #row_number:=0;
SET #dat_id = '';
SELECT *,
#row_number:=CASE WHEN #dat_id=dat_id THEN #row_number+1 ELSE 1 END AS row_number,
#dat_id:=dat_id AS dat_id_row_count
FROM data d
INNER JOIN keywords
ON keywords.key_id = d.dat_id
INNER JOIN prods
ON keywords.key_prod = prods.prod_id
WHERE d.row_number < 3
The other approach is self joining. I don't want to take credit for somebody else's job, so please look on the following example:
ROW_NUMBER() in MySQL
Look for the following there:
SELECT a.i, a.j, (
SELECT count(*) from test b where a.j >= b.j AND a.i = b.i
) AS row_number FROM test a
If you only want to do this for one key_id at a time (as alluded to in your responses to other answers) and only want two rows, you can just do:
SELECT p.prod_id,
p.prod_name,
p.prod_a_id,
p.prod_b_id,
k.key_id,
k.key_word,
k.key_country,
d.dat_date,
d.dat_rank_a,
d.dat_rank_b,
d.dat_traffic_a,
d.dat_traffic_b
FROM keywords k
JOIN data d
ON k.key_id = d.dat_id
JOIN prods p
ON k.key_prod = p.prod_id
WHERE k.key_id = :key_id /* Bind in key id */
ORDER BY d.dat_date DESC
LIMIT 2;
Whether you want this depends on your data structure and whether there is more than one key/prod combination per date.
Another option limiting just the data rows would be:
SELECT p.prod_id,
p.prod_name,
p.prod_a_id,
p.prod_b_id,
k.key_id,
k.key_word,
k.key_country,
d.dat_date,
d.dat_rank_a,
d.dat_rank_b,
d.dat_traffic_a,
d.dat_traffic_b
FROM keywords k
JOIN (
SELECT dat_id,
dat_date,
dat_rank_a,
dat_rank_b,
dat_traffic_a,
dat_traffic_b
FROM data
WHERE dat_id = :key_id /* Bind in key id */
ORDER BY dat_date DESC
LIMIT 2
) d
ON k.key_id = d.dat_id
JOIN prods p
ON k.key_prod = p.prod_id;
If you want some kind of grouped results for all the keywords, you'll need to look at the other answers.
I think a window function is the best way to go. without knowing a lot about the structure of the data you can try a subquery of what you are trying to restrict and then joining that to the rest of the data. Then within the where clause restrict the rows you pull back.
select p.prod_id, p.prod_name, p.prod_a_id, p.prod_b_id,
t.key_id, t.key_word, t.kay_country, t.dat_date,
t.dat_rank_a, t.dat_rank_b, t.dat_traffic_a, t.dat_traffic_b
from
(
select
k.key_id, k.key_word, k.kay_country, d.dat_date, d.dat_rank_a,
d.dat_rank_b, d.dat_traffic_a, d.dat_traffic_b,
row_number() over (partition by dat_id order by dat_date desc) as 'RowNum'
from keywords as k
inner join
data as d on k.key_id = d.dat_id
) as t
inner join
prods as p on t.key_prod = p.prod_id
where tmp.RowNum <=2
This is a "groupwise max" problem. Reference. CTE does not exist in MySQL.
I'm not totally clear on how your tables are linked, but here is a stab:
SELECT
*
FROM
( SELECT #prev := '', #n := 0 ) init
JOIN
( SELECT #n := if(k.key_id != #prev, 1, #n + 1) AS n,
#prev := k.key_id,
d.*, k.*, p.*
FROM data d
JOIN keywords k ON k.key_id = d.dat_id
JOIN prods p ON k.key_prod = p.prod_id
ORDER BY
k.key_id ASC,
d.dat_date ASC
) x
WHERE n <= 2
ORDER BY k.key_id, n;
you can use this query:
select prod_id, prod_name, prod_a_id, prod_b_id, key_id, key_word,
kay_country, dat_date, dat_rank_a, dat_rank_b, dat_traffic_a, dat_traffic_b
from keywords where dat_date in (
SELECT MAX(dat_date) FROM keywords temp_1
where temp_1.prod_id = keywords.prod_id
union all
SELECT MAX(dat_date) FROM keywords
WHERE dat_date NOT IN (SELECT MAX(dat_date ) FROM keywords temp_2 where
temp_2.prod_id = keywords.prod_id)
)

MySQL syntax for UPDATE + CASE WHEN + EXISTS + SELECT MAX

EDIT: Working now, solution in the bottom of this post.
I have tried to create this query for hours now, without success:
UPDATE tasks
SET `Order`=
(
CASE WHEN
(
SELECT EXISTS
(
SELECT 1
FROM user_job_tasks ujt
WHERE ujt.JobID = :jobID AND ujt.TaskID = LAST_INSERT_ID()
)
)
THEN
(
SELECT `order` FROM
(
SELECT MAX(t.`Order`)+1 AS `Order`
FROM user_job_tasks ujt
LEFT OUTER JOIN tasks t ON ujt.TaskID = t.ID
WHERE ujt.JobID = :jobID
) AS temp
)
ELSE
(
1
)
END
)
WHERE ID = LAST_INSERT_ID()
Now, the point of this is to update tasks table's column Order where ID equals something (LAST_INSERT_ID() here).
If any records exist in user_job_tasks where jobID and taskID match the values, I want to set Order as the highest value + 1 of Order found in user_job_tasks where jobID matches the value.
If not, Order will be set 1.
I am only getting an error General error: 1093 You can't specify target table 'tasks' for update in FROM clause. I fail to find anything wrong in the query syntax.
Sorry for confusing explanation, perhaps I should just go to sleep.
Working solution:
UPDATE tasks
SET `Order`=
(
COALESCE
(
(
SELECT `Order` FROM
(
SELECT MAX(t.`Order`)+1 AS `Order`
FROM user_job_tasks ujt
LEFT OUTER JOIN tasks t ON ujt.TaskID = t.ID
WHERE ujt.JobID = :jobID
) AS temp
),
1
)
)
WHERE ID = LAST_INSERT_ID()
I am not 100% sure this does what you want, but I think it does:
UPDATE tasks
SET `Order`= coalesce((SELECT MAX(t.`Order`) + 1 AS `Order`
FROM user_job_tasks ujt JOIN
tasks t
ON ujt.TaskID = t.ID
WHERE ujt.JobID = :jobID
), 1)
WHERE ID = LAST_INSERT_ID();
This just looks for the maximum id (+ 1) for your records and then uses 1 if this doesn't exist.
EDIT:
MySQL doesn't like using the table being updated in a from clause. So the above is correctly stated using a second subquery:
UPDATE tasks
SET `Order`= coalesce((SELECT `Order`
FROM (SELECT MAX(t.`Order`) + 1 AS `Order`
FROM user_job_tasks ujt JOIN
tasks t
ON ujt.TaskID = t.ID
WHERE ujt.JobID = :jobID
)
), 1)
WHERE ID = LAST_INSERT_ID();
This limitation would also apply to your query.

Mysql query optimization

HI im running socialengine3 and need optimization on the a custom Mutual friends query.
Its currently taking 15 seconds to execute
Friends table
friend_id
friend_user_id1
friend_user_id2
friend_status
friend_type
users
user_id
Edited
I have converted in into exists and still its now executing in 20 seconds.
below is the updated query.
SELECT friendlist.friend_user_id2, se_users.username, se_users.id, se_users.image, se_users.name, se_users.surname, count( * ) AS mutral_friends
FROM `se_friends` friendlist
INNER JOIN `users` se_users ON friendlist.friend_user_id2 = `se_users`.id
WHERE EXISTS (
SELECT se.friend_user_id2
FROM se_friends se
WHERE se.friend_user_id1 = '105012'
AND se.friend_status = '1'
AND se.friend_user_id2 = friendlist.friend_user_id1
) AND NOT EXISTS (
SELECT se1.friend_user_id2
FROM `se_friends` se1
WHERE se1.friend_user_id1 = '105012'
AND friendlist.friend_user_id2 = se1.friend_user_id2
)
AND NOT (
friendlist.friend_user_id2 = '105012'
)
AND friendlist.friend_status = '1'
GROUP BY friendlist.friend_user_id2, se_users.username, se_users.id, se_users.image, se_users.name, se_users.surname
ORDER BY mutral_friends DESC
LIMIT 0 , 20
Orignal query
SELECT DISTINCT `se_friends`.friend_user_id2, se_users.username, se_users.id, se_users.image, se_users.name, se_users.surname, count(*) as mutral_friends
FROM `se_friends`
INNER JOIN `users` se_users` ON `se_friends`.friend_user_id2=`se_users`.id
WHERE
(se_friends.friend_user_id1 <> '30355' or se_friends.friend_user_id2 <> '30355') AND
se_friends.friend_user_id1 IN
(SELECT se_friends.friend_user_id2
FROM `se_friends`
WHERE se_friends.friend_user_id1='".$user_id."' AND se_friends.friend_status='1')
AND `se_friends`.friend_user_id2 NOT IN
(SELECT se_friends.friend_user_id2
FROM `se_friends`
WHERE se_friends.friend_user_id1='".$user_id."'
)
AND NOT(se_friends.friend_user_id2='".$user_id."') AND se_friends.friend_status='1'
GROUP BY `se_friends`.friend_user_id2, se_users.username, se_users.id, se_users.image, se_users.name, se_users.surname
ORDER BY mutral_friends DESC
LIMIT 0, 20
IN is very expensive operation.
try to replace it with EXISTS. eg
select * from table where user_id in (select user_id from users where active='A')
and
select * from table t where exists (select user_id from users u where t.user_id = u.user_id and u.active='A')
if it won't be helpful, it's better to look at execution plan

Help on MySQL query

I have the following table structure:
Customers - Cust_Orders - Cust_Items - Cust_Payments - Drivers
id id id id id
company cid oid oid name
driver price amount
date qty date
vat
What I want to do is showing last unpaid order marked by a specific driver id + the sum of all unpaid orders for that particular customer except the order that is already selected.
Since there might be more than one cust_items & more than one cust_payments I had to use select from select as otherwise I would have wrong sums & things got messy till I reached a point I forgot what I was doing.
Any Help would be greatly appreciated.
My current SQL which lacks the final part only (sum of other unpaid orders amounts):
SELECT `customers`.`company`,
T1.*,
ROUND( IFNULL( SUM(`cust_payments`.`amount`), 0 ), 2) AS `paid`
FROM (
SELECT `cust_orders`.*,
ROUND( IFNULL( SUM(`cust_items`.`qty` * `cust_items`.`price`), 0 ), 2) AS `total`,
SUM( ( `cust_items`.`price` * `cust_items`.`qty` * `vat` ) / 100) AS `vat`
FROM `cust_orders`
LEFT JOIN `cust_items` ON `cust_orders`.`id` = `cust_items`.`oid`
GROUP BY `cust_orders`.`id`
) `T1`
LEFT JOIN `customers` ON `T1`.`cid` = `customers`.`id`
LEFT JOIN `cust_payments` ON `T1`.`id` = `cust_payments`.`oid`
WHERE `T1`.`driver` = ? GROUP BY `T1`.`id` HAVING (`T1`.`total` - `paid`) > ?
ORDER BY `T1`.`id` DESC LIMIT 1
Can you try
SELECT
x.id,
x.company,
y.id,
y.cid,
y.driver,
y.date,
#ut:=ROUND(SUM(z.qty*z.price),2) AS unpaid_total,
#uv:=SUM((#ut*z.vat)/100) AS unpaid_vat,
#st:=ROUND(SUM(b.qty*b.price),2)-#ut AS sum_total,
SUM((#st*b.vat)/100)-#uv AS sum_vat
FROM Customers x
INNER JOIN Cust_Orders y ON x.id=y.cid
INNER JOIN Cust_Items z ON y.id=z.oid
LEFT JOIN Cust_Orders a ON x.id=a.cid
LEFT JOIN Cust_Items b ON a.id=b.oid
WHERE
y.driver=? AND
NOT EXISTS (SELECT * FROM Cust_Payments WHERE oid=y.id) AND
NOT EXISTS (SELECT * FROM Cust_Payments WHERE oid=a.id)
GROUP BY x.id,x.company, y.id, y.cid, y.driver, y.date