###前提・実現したいこと
date name pay
2015/2/1 tarou 5,000
2015/2/2 zirou 8,000
2015/2/2 saburou 9,000
2015/2/2 tarou 1,000
上記のようなデータが入ってるテーブルに対し
2015/2/1のtarouから2015/2/2tarouまでの間のデータ抽出するには
どのようなクエリを書けばよいのでしょうか?
結論下記を出したいです。
2015/2/2 zirou 8,000
2015/2/2 saburou9,000
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答2件
0
ちなみに1回目のtarouから2回目のtarouまでの間のデータで
日付を指定せずに出せるとうれしいです。
2015/2/1 tarou 5,000 2015/2/2 zirou 8,000 2015/2/2 saburou 9,000 2015/2/2 tarou 1,000
このデータの並びがそもそもどういう並び順になっているのかわからないと正確な解はわかりませんが、id のような連番のフィールドがあると仮定するなら。
まずデータを用意(こういうのを質問に書いておくと回答がつきやすいですよ)。
lang
1create table t ( 2 id int not null primary key auto_increment, 3 date date not null, 4 name text not null, 5 pay int not null 6); 7 8insert into t values (null, '2015/2/1', 'tarou', 5000); 9insert into t values (null, '2015/2/2', 'zirou', 8000); 10insert into t values (null, '2015/2/2', 'saburou', 9000); 11insert into t values (null, '2015/2/2', 'tarou', 1000); 12insert into t values (null, '2015/2/2', 'ore', 9999); 13insert into t values (null, '2015/2/3', 'are', 9999); 14insert into t values (null, '2015/2/3', 'tarou', 1000); 15insert into t values (null, '2015/2/3', 'sore', 9999);
サブクエリで1番目と2番目をそれぞれ取得して条件に使用。
select * from t where id > (select id from t where name = 'tarou' order by id limit 1) and id < (select id from t where name = 'tarou' order by id limit 1,1) order by id ;
あるいは、1番目と2番目を含むサブクエリを結合して having で絞る。
lang
1select t.id, t.date, t.name, t.pay 2from t join (select id from t where name = 'tarou' order by id limit 2) as x 3group by t.id, t.date, t.name, t.pay 4having t.id > min(x.id) and t.id < max(x.id) 5;
投稿2015/06/12 05:55
編集2015/06/12 09:36総合スコア4514
0
Ans
select * from ( (select * from `table` where name = 'zirou' order by date asc limit 1) union all (select * from `table` where name = 'saburou' order by date asc limit 1) ) as a where date >= '2015-02-01 00:00:00' and date < '2015-02-03 00:00:00';
追記
--> の部分のみ抽出したい認識で間違いありませんか?
2015/2/1 tarou 5,000 --> 2015/2/2 zirou 8,000 --> 2015/2/2 saburou 9,000 2015/2/2 tarou 1,000
下記旧回答
Ans
lang
1select * from table_name 2where date >= '2015-02-01 00:00:00' 3and date < '2015-02-02 00:00:00' 4and name IN('zero', 'sabre');
備考
下記のように書く事も可能です。
select * from table_name where date >= '2015-02-01 00:00:00' and date <= '2015-02-01 23:59:59' and name IN('zero', 'sabre');
取得するカラムを絞る例
select name from table_name where date >= '2015-02-01 00:00:00' and date < '2015-02-02 00:00:00' and name IN('zero', 'sabre');
また、betweenを使用した方法等、答えは一つではありません。
下記を参考にしてみてください。
参考
MySQL Manual
http://mysql.stu.edu.tw/doc/refman/5.1/ja/comparison-operators.html
betweenを使用してdatetime検索
http://fukaoi.org/2009/03/19/mysql_datetime
投稿2015/06/12 05:11
編集2015/06/12 06:24退会済みユーザー
総合スコア0
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/06/12 05:37
退会済みユーザー
2015/06/12 06:13
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。