简体   繁体   English

sql 如何使用选择查询来获取调用外键

[英]sql how to use select query to get invoke foreign key

i was wondering how do i use the select query to Retrieve all offers of a listing .我想知道如何使用选择查询来检索列表的所有报价

Any tips or help to improve my codes is appreciated to!感谢任何提示或帮助改进我的代码! Thank you and have a nice day!谢谢你,祝你有美好的一天!

CREATE DATABASE  IF NOT EXISTS `assignment_db`;
USE `assignment_db`;

CREATE TABLE USER_LIST(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
userName VARCHAR(50) NOT NULL,
email varchar(100) NOT NULL,
registeredDate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);


create table listing_list(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
itemName VARCHAR(50) NOT NULL,
itemDescription VARCHAR(254) NOT NULL,
price DECIMAL(4,2) NOT NULL,
fk_poster_id int references USER_LIST(id),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);

create table offer_list(
id int(6) Unsigned auto_increment Primary key,
offer int,
fk_listing_id int references listing_list(id),
fk_offeror_id int references user_list(id),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);

insert into user_list (userName, email) values ('John','johnnyboi@123.com');
insert into user_list (userName, email) values ('Tom','Tommyboi@123.com');
insert into listing_list (itemName,itemDescription, price) values ( 'Pen', 'A long delicate pen.',' 1.50 ');
insert into listing_list (itemName,itemDescription, price) values ( 'Pencil', 'A long delicate pencil.',' 0.50 ');
insert into offer_list (offer,fk_listing_id,fk_offeror_id) values ('200','2','3');
insert into offer_list (offer,fk_listing_id,fk_offeror_id) values ('200','1','1');

All offer listing for existing user :现有用户的所有优惠列表:

select a.*,b.*,c.* from offer_list a
INNER JOIN (listing_list as b)
on a.fk_listing_id=b.Id

INNER JOIN (user_list as c)
on a.fk_offeror_id=c.Id

just all offer listing :只是所有报价清单

select a.*,b.* from listing_list b
INNER JOIN (offer_list as a)
on a.fk_listing_id=b.Id

just all offer listing 2nd way只是所有提供上市第二种方式

select a.*,b.* from offer_list a
 INNER JOIN (listing_list as b)
on a.fk_listing_id=b.Id

Check it here , i tested them in this FIDDLE在这里检查,我在这个FIDDLE 中测试了它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM