CREATE TABLE User (
id INTEGER NOT NULL AUTO_INCREMENT,
login VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
status INTEGER NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE Message (
id INTEGER NOT NULL AUTO_INCREMENT,
userid INTEGER NOT NULL,
message VARCHAR(255) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (userid) REFERENCES User(id)
);
SELECT order_number, order_status, order_date
FROM (
SELECT order_number, order_status, order_date,
RANK() OVER(PARTITION BY order_number ORDER BY order_date DESC) AS r
FROM orders
) t
WHERE r = 1
SELECT id
FROM tbl
GROUP BY id
HAVING MIN(discount) = 'PROMO' AND MAX(discount) = 'PROMO'
select `order`, item, count(discount)
from store
group by `order`, item
having count(discount) = count(case when discount='PROMO' then 1 end)
CREATE TABLE users (
id int primary key auto_increment,
name varchar(64)
);
CREATE TABLE favorites (
user_id int references users(id),
bookmark varchar(255),
primary key (user_id, bookmark)
);
CREATE TABLE statuses (
id int primary key auto_increment,
title varchar(64)
);
CREATE TABLE orders (
id int primary key auto_increment,
created_at datetime default now(),
status_id int references statuses(id)
);
select *
from table1
left join table2 on table1.code = table2.id1
where table1.code = 1;