I have 2 tables in MySQL databased named order and order_item.
An order can have multiple order_items in it. So I am creating one order table and one order_item table (mapped with a foreign key to order table). Please see the SQLFiddle link and the schema below.
create table if not exists order_info(
order_id int not null AUTO_INCREMENT,
delivery_address varchar(100),
created_at timestamp DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (order_id)
);
create table if not exists order_items(
item_id int not null AUTO_INCREMENT,
order_id int,
name varchar(100),
FOREIGN KEY (order_id) REFERENCES order_info(order_id),
PRIMARY KEY (item_id)
);
INSERT order_info(order_id, delivery_address, created_at)
VALUES (1, "NYC Manhattan", '2021-12-27 12:05:32'),
(2, "NYC Brooklyn", '2021-12-27 11:05:23'),
(3, "NYC Bronx", '2021-12-26 13:09:23');
INSERT order_items(item_id,order_id,name)
VALUES (1, 1, "Shoes"),
(2, 1, "Basketballs"),
(3, 1, "Baseballs"),
(4, 2, "Boxing gloves"),
(5, 3, "Baseball caps"),
(6, 3, "Nike T-shirts");
I am looking for responses which combine these 2 tables but with a count limit imposed on the number of items.
E.g. I am looking for the results in the format of a list with one order_item per order.
order_id, delivery_address, item_id, item.name
So my results will be like
[{1, NYC Manhattan, 1, Shoes},
{1, NYC Manhattan, 2, Basketballs}]
Suppose I want to see last n number of order items placed in NYC Bronx. I understand that it might lead to cases which contains returning partial orders.
''
My questions are:
- Is it a reasonable way to fire queries to the database? Is my database design good enough to support such queries?
- I have tried a lot of query but can't make any query work which can impose limits on the count of rows in order_items.