Max ID を取得する方法を知っています。簡単だ。しかし、MaxID が 1 の値を持つ行である可能性がある状況があります。例..次のテーブルがあります。
CREATE TEMPORARY TABLE people (
id INT NOT NULL
,NAME VARCHAR(50) NOT NULL
);
INSERT INTO people (id, NAME) VALUES (1, 'tony');
INSERT INTO people (id, NAME) VALUES (2, 'dave');
INSERT INTO people (id, NAME) VALUES (3, 'dan');
CREATE TEMPORARY TABLE orders (
id INT NOT NULL
,peopleid INT NOT NULL
,VALUE INT
);
INSERT INTO orders (id, peopleid, VALUE) VALUES (1, 1, NULL);
INSERT INTO orders (id, peopleid, VALUE) VALUES (2, 1, 1);
INSERT INTO orders (id, peopleid, VALUE) VALUES (3, 1, NULL);
INSERT INTO orders (id, peopleid, VALUE) VALUES (4, 2, NULL);
INSERT INTO orders (id, peopleid, VALUE) VALUES (5, 2, NULL);
INSERT INTO orders (id, peopleid, VALUE) VALUES (6, 2, NULL);
INSERT INTO orders (id, peopleid, VALUE) VALUES (7, 2, NULL);
INSERT INTO orders (id, peopleid, VALUE) VALUES (8, 1, NULL);
INSERT INTO orders (id, peopleid, VALUE) VALUES (9, 2, NULL);
クエリを実行すると:
SELECT * FROM people AS p
LEFT JOIN orders AS o ON o.peopleid = p.id
私は結果を得る:
id name id peopleid value
1 tony 1 1 null
1 tony 2 1 1
1 tony 3 1 null
1 tony 8 1 null
2 dave 4 2 null
2 dave 5 2 null
2 dave 6 2 null
2 dave 7 2 null
2 dave 9 2 null
3 dan null null null
結果が必要です
id name id peopleid value
1 tony 2 1 1
2 dave 9 2 null
3 dan null null null