0

私は完全な SQL 初心者です。ごめん。

私はテーブルを持っています

mysql> describe activity;
+--------------+---------+------+-----+---------+-------+
| Field        | Type    | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+-------+
| user         | text    | NO   |     | NULL    |       |
| time_stamp   | int(11) | NO   |     | NULL    |       |
| activity     | text    | NO   |     | NULL    |       |
| item         | int     | NO   |     | NULL    |       |
+--------------+---------+------+-----+---------+-------+

通常activityは 2 段階のプロセスです。1)「チェックアウト」と2「使用」

item使用しない限り、再度チェックアウトすることはできません。

ここで、アイテムがチェックアウトされたが使用されなかったケースを見つけたいと考えています。

ばかげているので、同じアイテムに対してチェックアウト用と使用用の 2 つの選択を使用し、タイムスタンプを比較します。

チェックアウトされたが使用されていないアイテムを選択するのに役立つ SELECT ステートメントはありますか?

マルチペル チェックアウトの可能性があるため注意が必要です。それとも、コーディングするだけですか

loop over activity, form oldest until newset
  if find a checkout and there is no newer used time then i have a hit 
4

1 に答える 1

2

You could get the last date of each checkout or use and then compare them per item:

SELECT MAX(IF(activity='check out', time_stamp, NULL)) AS last_co, 
MAX(IF(activity='use', time_stamp, NULL)) AS last_use
FROM activity
GROUP BY item
HAVING NOT(last_use >= last_co);

The NOT(last_use >= last_co) is written that way because of how NULL compare behaviour works: last_use < last_co will not work if last_use is null.

Without proper indexing, this query will not perform very well though. Plus you might want to bound the query using a WHERE condition.

于 2012-04-30T07:27:24.910 に答える