Skip to content

Instantly share code, notes, and snippets.

@Semior001
Created June 1, 2024 21:12
Show Gist options
  • Save Semior001/7f45e81d9207e1206fe53a3508a2a2f9 to your computer and use it in GitHub Desktop.
Save Semior001/7f45e81d9207e1206fe53a3508a2a2f9 to your computer and use it in GitHub Desktop.
id price name
0 12345 abacaba
0 12345 cabaaba
1 54321 wtf
create table 'orders' (
id integer primary key autoincrement,
customer_id int not null
);
create table 'prices' (
id integer primary key autoincrement,
price int not null
);
create table 'items' (
id integer primary key autoincrement,
order_id int not null,
name text not null default '',
price_id int not null,
constraint fk_order_id foreign key (order_id) references orders(id),
constraint fk_price_id foreign key (price_id) references prices(id)
);
insert into prices (id, price) values (0, 12345);
insert into prices (id, price) values (1, 54321);
insert into orders (id, customer_id) values (0, 0);
insert into orders (id, customer_id) values (1, 0);
insert into orders (id, customer_id) values (2, 1);
insert into orders (id, customer_id) values (3, 1);
insert into items (id, order_id, name, price_id) values (0, 0, 'abacaba', 0);
insert into items (id, order_id, name, price_id) values (1, 0, 'cabaaba', 0);
insert into items (id, order_id, name, price_id) values (2, 1, 'wtf', 1);
insert into items (id, order_id, name, price_id) values (3, 2, 'not shown', 1);
select orders.id, prices.price, items.name from items
join prices on items.price_id = prices.id
join orders on orders.id = items.order_id
where orders.customer_id = ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment