LoginSignup
8
9

More than 5 years have passed since last update.

みんなの為のMySQLまとめ(5)

Last updated at Posted at 2015-02-07

参考
http://homepage2.nifty.com/sak/w_sak3/doc/sysbrd/mysql_10.htm

■件数指定 LIMIT

・先頭から 3 件問い合わせるには、次のようにする。
access の top と同じことができる。
(TOP 10 取得、表示行制限、レコード数限定、件数指定、表示件数)
(1 レコード取得、1 件取得)

select * from testm
order by key1
limit 0, 3
;
kobito.1423327482.730568.png

・先頭レコードを問い合わせるには、次のようにする。

select * from testm
where key1 like 'a%'
order by key1
limit 0, 1
;
kobito.1423327701.873612.png

・最後から 3 件問い合わせるには、ソートオーダを逆にすれば良い。

select * from testm
order by key1 desc
limit 0, 3
;
kobito.1423327793.463747.png
 

■HAVING

・集計結果に対する問い合わせ条件を指定する。

select key1, count(*), sum(data1), avg(data1) from testm
group by key1
having sum(data1) > 10;
kobito.1423327998.945069.png

■CROSS JOIN

・Oracle の「,」標記と同じ。(クロス結合、クロスジョイン)

select * from test2m cross join testm
where test2m.code1 = testm.key1;
kobito.1423328475.028625.png

■NATURAL JOIN

・同じ名前の列を自然結合する。(ナチュラルジョイン)
(この例は悪い。)

select * from test2m natural left join testm;
kobito.1423328540.133249.png

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9