データベース切り替え
\c DB名
データベース一覧
\l
テーブル一覧
\dt
カラム一覧
\d DB名
データーベース作成
CREATE DATABASE name;
テーブル内のデータをすべて削除
TRUNCATE テーブル名;
内部結合
キーがなければ行は削除される
select
m.id as m_id,
m.kaisya as m_kaisya,
m.gurupu_no as m_gurupu_no,
g.gurupu_no as g_gurupu_no,
g.id as g_id,
g.name as g_name,
g.syumi as g_syumi
from test m
join testgg g on m.gurupu_no = g.gurupu_no;
外部結合
キーがなくても表示される
select
m.id as m_id,
m.kaisya as m_kaisya,
m.gurupu_no as m_gurupu_no,
g.gurupu_no as g_gurupu_no,
g.id as g_id,
g.name as g_name,
g.syumi as g_syumi
from test m
left join testgg g on m.gurupu_no = g.gurupu_no
サブクエリとjoin
SELECT
testgg.name AS ggname,
testgg.syumi AS ggsyumi,
testgg.gurupu_no AS gg_gurupu_no,
test.kaisya,
test.id
FROM testgg
INNER JOIN (
SELECT *
FROM test
WHERE test.id = 2
) AS test ON test.gurupu_no = testgg.gurupu_no;