gpt4 book ai didi

mysql - 我在 MYSQL 查询中的 SELF JOIN 中遇到问题。请查表并查询

转载 作者:行者123 更新时间:2023-11-29 10:04:13 26 4
gpt4 key购买 nike

请通过以下查询创建表:

 CREATE TABLE `trade` 
(
`order` VARCHAR(10) DEFAULT NULL,
`positionid` INT(11) DEFAULT NULL,
`time` DATETIME DEFAULT NULL,
`volume` FLOAT NOT NULL
)
engine=innodb
DEFAULT charset=latin1;

插入查询

 INSERT INTO `trade` 
(`order`,
`positionid`,
`time`,
`volume`)
VALUES ('42556',
1111,
'2018-08-15 07:27:44',
2),
('42560',
1111,
'2018-08-18 08:32:47',
2),
('42564',
1235,
'2018-08-21 07:10:12',
5),
('42572',
1235,
'2018-08-23 17:20:26',
2),
('42580',
1235,
'2018-08-23 17:03:30',
3);

我尝试过以下查询:

 SELECT b.`order` AS `TICKET`, 
b.`time` AS `OPEN_TIME`,
j.`time` AS `CLOSE_TIME`
FROM trade AS b
LEFT JOIN trade AS j
ON b.`positionid` = j.`positionid`
WHERE b.`time` != j.`time`;

表:

enter image description here

错误输出:

该图像显示重复数据:

enter image description here

所需输出:

此图像将显示所需的输出:

enter image description here

描述:

当我们打开交易时,它将存储到交易表中。之后,当我们关闭交易时,它将以不同的时间和交易量进行另一个输入(如果交易将部分关闭,则交易量将不同,否则将完全关闭交易)。这里第一个条目是 OPEN_TIME,存储为 Time,第二个条目是 CLOSE_TIME。那么如何将两个或多个记录转换为具有 OPEN_TIME 和 CLOSE_TIME 的单个记录呢?

最佳答案

检查这个

SELECT j.`order` AS `TICKET`, 
b.`positionid` AS `PositionID`,
b.`time` AS `OPEN_TIME`,
j.`time` AS `CLOSE_TIME` ,
j.`volume` AS `Volume`
FROM trade AS b
INNER JOIN trade AS j
ON b.`positionid` = j.`positionid`
where b.time = (select min(time) from trade as c1 where b.positionid=c1.positionid)
AND b.time!=j.time;

关于mysql - 我在 MYSQL 查询中的 SELF JOIN 中遇到问题。请查表并查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52252865/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com