我在PHP 5.5.9中使用SQLite3
这是我正在使用的查询
SELECT ForignKey_A, Location_ID, TimeStamp
FROM Events
GROUP BY ForignKey_A
ORDER BY TimeStamp
我希望Location_ID
在TimeStamp
所在的行上获得ForignKey_A
每个TimeStamp
的最小值和/或最大值(如果我可以同时执行一旦那很好)
我想我会得到组中的第一个值,具体取决于我如何排序type
但它不起作用,每组通常有2到6行
我不确定这个专栏是否对此有用,但是
我的列名为type
,布尔值为1/0
每个MIN(TimeStamp)
组中ForignKey_A
行的type
行= 0。
同样MAX(TimeStamp)
将在每个ForignKey_A
组中╔═════════════╦═════════════╦════════════╦══════╗
║ ForignKey_A ║ Location_ID ║ TimeStamp ║ Type ║
╠═════════════╬═════════════╬════════════╬══════╣
║ 1 ║ 762 ║ 1496062971 ║ 0 ║
║ 1 ║ 427 ║ 1496063971 ║ 1 ║
║ 1 ║ 417 ║ 1496065971 ║ 0 ║
║ 1 ║ 123 ║ 1496072971 ║ 1 ║
║ 2 ║ 594 ║ 1496062971 ║ 0 ║
║ 2 ║ 427 ║ 1496072971 ║ 1 ║
║ 3 ║ 217 ║ 1496082971 ║ 0 ║
║ 3 ║ 356 ║ 1496092971 ║ 1 ║
║ 3 ║ 985 ║ 1496099971 ║ 0 ║
║ 3 ║ 789 ║ 1496162971 ║ 1 ║
║ 3 ║ 456 ║ 1496262971 ║ 0 ║
║ 3 ║ 123 ║ 1496362971 ║ 1 ║
╚═════════════╩═════════════╩════════════╩══════╝
行的行中= 1
Location_ID
我确实知道如果我这样做,我会得到相关的SELECT MIN(TimeStamp), Location_ID
FROM TableA
GROUP BY ForignKey_A
TimeStamp
我可以从与MIN / MAX相同的行中获取数据Location_ID
这让我得到了我需要的东西,我有几个子选择查询,但是它有效,但有没有办法做这样的事情并得到正确的SELECT MIN(TimeStamp) AS 'Begin', Location_ID AS 'Location_ID @ MIN TimeStamp',
MAX(TimeStamp) AS 'End', Location_ID AS 'Location_ID @ MAX TimeStamp',
ForignKey_A
FROM TableA
GROUP BY ForignKey_A
?
╔═════════════╦═══════╦════════════╦══════╦════════════╗
║ ForignKey_A ║ Start ║ Begin ║ Stop ║ End ║
╠═════════════╬═══════╬════════════╬══════╬════════════╣
║ 1 ║ 762 ║ 1496062971 ║ 123 ║ 1496072971 ║
║ 2 ║ 594 ║ 1496062971 ║ 427 ║ 1496072971 ║
║ 3 ║ 217 ║ 1496082971 ║ 123 ║ 1496362971 ║
╚═════════════╩═══════╩════════════╩══════╩════════════╝
这会产生类似的结果
<?php
$app->put('/hdc/v1/pagamento/{pag_id}', function($pag_id, Request $request) use ($app) {
return $app->json(array('message' => 'error'), 400);
});
答案 0 :(得分:1)
使用单个查询,无法将MIN()/ MAX()与列值相关联。 您必须使用子查询查找最小值和最大值,然后将这些值连接在一起:
SELECT ForeignKey_A,
b.TimeStamp,
b.Location_ID,
e.TimeStamp,
e.Location_ID
FROM (SELECT ForeignKey_A,
min(TimeStamp) AS TimeStamp,
Location_ID
FROM TableA
GROUP BY ForeignKey_A
) AS b
JOIN (SELECT ForeignKey_A,
max(TimeStamp) AS TimeStamp,
Location_ID
FROM TableA
GROUP BY ForeignKey_A
) AS e
USING (ForeignKey_A);