简体   繁体   English

更新表中的订单值-MySQL选择查询具有多个联接

[英]Update order value in table - mysql select query has multiple joins

I'm pretty good with mySQL but for some reason I'm struggling to get my head around this one. 我对mySQL相当满意,但是由于某种原因,我一直在努力使自己的头脑变得清晰。

I need to programmatically update the order values in the wp_esp_ticket table as the dates get out of order sometimes. 由于日期有时会混乱,因此我需要以编程方式更新wp_esp_ticket表中的订单值。 This is the query I have to use to bring back all the dates that are related to each other and need ordering: 这是我必须用于带回彼此相关且需要排序的所有日期的查询:

SELECT * FROM `wp_esp_ticket` et 
INNER JOIN wp_esp_datetime_ticket edtt on edtt.TKT_ID = et.TKT_ID
INNER JOIN wp_esp_datetime edt on edtt.DTT_ID = edt.DTT_ID
WHERE edt.EVT_ID = 1325

This brings back all the rows that need updating in the correct order, but the TKT_order column in the wp_esp_ticket table is then out of sequence. 这将带回所有需要以正确顺序进行更新的行,但是wp_esp_ticket表中的TKT_order列却不按顺序。 I need to give the top row a value of 1 in the column TKT_order and go up in increments of 1 for each row below. 我需要在TKT_order列中为最上面的行赋予1的值,并为下面的每一行以1的增量递增。

I need to put together a tidy sql statement for this as it will need to be run along with another peice of code that adds new dates to the database. 我需要为此整理一条整齐的sql语句,因为它将需要与向数据库添加新日期的另一段代码一起运行。

Thanksss!!! Thanksss!

Update: 更新:

Thanks to the input and direction from Drew I got this to work: 多亏了Drew的投入和指导,我才得以完成这项工作:

SET @newnum = 0;

Update wp_esp_ticket tix

INNER JOIN (
SELECT TKT_ID FROM `wp_esp_ticket` et 
INNER JOIN wp_esp_datetime_ticket edtt on edtt.TKT_ID = et.TKT_ID
INNER JOIN wp_esp_datetime edt on edtt.DTT_ID = edt.DTT_ID
WHERE edt.EVT_ID = 1325
ORDER BY edt.DTT_EVT_start ASC
) b ON tix.TKT_ID = b.TKT_ID

SET tix.TKT_order = @newnum:=@newnum + 1

From the Manual near the top of the page. 从页面顶部附近的手册中

For example, if the table contains 1 and 2 in the id column and 1 is updated to 2 before 2 is updated to 3, an error occurs. 例如,如果表在id列中包含1和2,并且在2更新为3之前将1更新为2,则会发生错误。 To avoid this problem, add an ORDER BY clause to cause the rows with larger id values to be updated before those with smaller values: 为避免此问题,请添加一个ORDER BY子句,以使具有较大id的行在具有较小id的行之前进行更新:

UPDATE t SET id = id + 1 ORDER BY id DESC; 更新t SET ID = ID + 1 ORDER BY ID DESC;

Note that is a conceptual from the manual. 请注意,这是手册中的概念。 Tweak accordingly. 相应地进行调整。

Combine the update / order by with the concept of update with a join ( How to do 3 table JOIN in UPDATE query? ) and you are all set. 将更新/顺序与具有联接update with a join概念结合update with a join如何在UPDATE查询中执行3表JOIN? ),一切就绪。

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

In your case, I diverge, going with a rownum: 在您的情况下,我分歧了,以rownum开头:

set @rownum:=0;

update `wp_esp_ticket` et 
INNER JOIN wp_esp_datetime_ticket edtt on edtt.TKT_ID = et.TKT_ID
INNER JOIN wp_esp_datetime edt on edtt.DTT_ID = edt.DTT_ID
set TKT_order =@rownum:=@rownum+1
WHERE edt.EVT_ID = 1325

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

see this rough example in action:

create table j
(   k varchar(20) not null,
    theOrder int not null
);

insert j(k,theOrder) values ('z',-1),('gj',-1),('w',-1),('h',-1),('uw',-1),('b',-1);

set @rownum:=0;
update j
set theOrder=@rownum:=@rownum+1
order by k;

+----+----------+
| k  | theOrder |
+----+----------+
| b  |        1 |
| gj |        2 |
| h  |        3 |
| uw |        4 |
| w  |        5 |
| z  |        6 |
+----+----------+

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Not sure if TKT_order is a existing column or a calculated column. 不确定TKT_order是现有列还是计算列。 If it's an existing column then what if you add a order by clause on TKT_order 如果它是现有列,那么如果您在TKT_order上添加order by子句,该TKT_order

SELECT * FROM `wp_esp_ticket` et 
INNER JOIN wp_esp_datetime_ticket edtt on edtt.TKT_ID = et.TKT_ID
INNER JOIN wp_esp_datetime edt on edtt.DTT_ID = edt.DTT_ID
WHERE edt.EVT_ID = 1325
ORDER BY et.TKT_order; <-- This one

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM