MySQL で LIMIT句を利用して指定した件数のみ処理する

2010年11月18日 12:02 MySQL で LIMIT句を利用して指定した件数のみ処理する

MySQL で、指定した件数のみ何らかの処理を行いたいときは、LIMIT句を利用します。

SELECT文の場合

ORDER BY句の次に LIMIT句を書いて、件数指定します。
ORDER BY句は使用しなくても構いませんが、使用しないと当然意図した順番通りに抽出することができません。

最初の50件のみを SELECT文で抽出したい場合は、

select * from table_name order by user_id limit 50;

と書けば、user_id の昇順で、50件のみ抽出されます。

また、50件目以降のデータを100件抽出したい、というような場合は、

select * from table_name order by user_id limit 50, 100;

と書くことで、実現できます。

UPDATE文・DELETE文の場合

SELECT文と同様に、ORDER BY句の次に LIMIT句を書いて、件数指定します。
こちらも同様に、ORDER BY句は使用しなくてもいいですが、意図した順番通りの処理ができないので結果は SELECT文以上に致命的ですw

update table_name set user_flag = 1 order by user_entry_date limit 50;
delete from table_name order by user_entry_date limit 50;

上記のように書くと、例えば user_entry_date が古いデータから順に50件に、user_flag を立てる、データを消す、というような処理を行なえます。

なお、UPDATE文や DELETE文では、残念ながら、SELECT文のように「limit 50,100」というような指定はできないようです。

阿部辰也へのお仕事のご依頼・お問合せはこちら

Twitter始めました。Followはお気軽にどうぞ。

関連するブログ記事
スポンサード リンク
カテゴリー
MySQL
タグ
DELETE | LIMIT | MySQL | ORDER BY | RDBMS | SELECT | SQL | UPDATE
現在位置
TOP > Web制作技術 > MySQL > MySQL で LIMIT句を利用して指定した件数のみ処理する
前のブログ記事
2010年10月の読了本まとめ [2010年11月13日 12:51]
次のブログ記事
2010年11月の読了本まとめ [2010年12月 4日 19:36]

トラックバック(0)

このブログ記事に対するトラックバックURL:

コメント(15)

gjfkyfcu [2013年12月30日 07:03]

dnowyvlh

discover this [2013年12月31日 04:46]

iouqdsyd http://paydayloansusaplf.com/ discover this

hello [2013年12月31日 06:08]

uqxykjdg uulvva

hello [2013年12月31日 07:36]

dojrpddx [url=http://ksanbz.com/]nmkbdo[/url]

hello [2013年12月31日 08:57]
money mutual [2014年9月22日 21:14]

qtarnjg http://paydayloansilk.com/ money mutual

ativan [2015年4月28日 22:14]
fzrbdp [2015年5月19日 05:05]

lcbkebi

hello [2015年5月20日 16:28]

auspmsav cdplos

hello [2015年5月20日 19:13]

yxddvtx [url=http://pzfsct.com/]vupvgf[/url]

hello [2015年5月20日 20:51]
Mosaicatl.com [2015年12月15日 15:46]

You should comment on the competition comparison of the blog. You can highlight it's mind boggling. Your blog exploration/tour will broaden your conversions.

Japanhotelrooms.blogspot.com [2015年12月18日 12:25]

This is my initial time i visit here. I found such a large amount of amusive stuff in your web log, particularly its discussion. From the a lot of comments on your articles, i assume i'm not the sole one having all the leisure here! sustain the great work. I actually have been desiring to write one thing like this on my web site and you have got given Maine a concept.

Iunofww [2017年1月17日 07:30]

As you can woo from the blood sway design, only harmonious of the numbers has to be higher viagra without a doctor prescription in us or lessen than it should be to count as either extraordinary blood bring pressure to bear on or murmured blood crushing:

xwpxjh [2017年1月28日 04:44]

The nicest attest indicates that momentous blood insistence does not cause headaches or nosebleeds, except in the box of hypertensive turning-point, a medical emergency when blood tension is 180/110 mm Hg or higher. If your blood burden is unusually on a trip viagra without a doctor prescription you be struck by problem or nosebleed and are feeling unwell, on the back burner serve five minutes and retest. If your reading remains at 180/110 mm Hg or higher, holler 9-1-1.

コメントする


画像の中に見える文字を入力してください。

 

このブログについて

阿部辰也

Webコンサルタントやシステムエンジニア、執筆業などをやっている、グッドホープ代表・阿部辰也のブログです。
Web技術系のTipsから仕事の話、趣味の話まで色々と。
人生は所詮生まれてから死ぬまでの壮大な暇つぶし。
だったら有意義に暇をつぶして生きましょー。

阿部辰也へのお仕事のご依頼・問合せはこちらからどうぞ。

Twitter: @t_abe
読書メーター: ID:Tatsuya

このブログのサイトマップ
このブログのはてなブックマーク数

Feed/RSS購読

タグクラウド

最近のトラックバック

阿部辰也の著書

77のキーワードで知る インターネットで稼ぐ 図解 ブログ・メルマガ・Webサイト構築の基本

77のキーワードで知る インターネットで稼ぐ 図解 ブログ・メルマガ・Webサイト構築の基本

頑張って書いたのに、出版後すぐに出版社が倒産して印税も貰えずじまいで俺涙目(笑)。

Powered by Movable Type 5.02

スポンサード リンク

はてなブックマーク



あわせて読みたい

t-abeの今読んでる本

t-abeの最近読んだ本

t-abeの今やってるゲーム

t-abeの最近やったゲーム