我将从MYSQL中的所有表中生成特定信息。我的数据库里有5张桌子。即:
advertiser ( Adv\_id int(11) NULL AUTO_INCREMENT,Name char(20) NULL,F\_Name char(20) NULL,Address varchar(40) NULL,CNIC int(13) NULL,Contact int(11) NULL,Monthly\_fee varchar(10) NULL,Region varchar(10) NULL),Reg\_date varchar(10) NULL,主键(Adv\_id) ) ENGINE=InnoDB AUTO_INCREMENT=3默认CHARSET=latin1;company\_information ( Company\_id int(11) NULL AUTO_INCREMENT,Company\_Name varchar(20) NULL,Company\_Contact int(11) NULL,Company\_Address varchar(30) NULL,主键(Company\_id) ) ENGINE=InnoDB默认CHARSET=latin1;advertisement ( Ads\_id int(11) NULL AUTO_INCREMENT,Adv\_id\_id int(11) NULL,Company\_id int(11) NULL,Ads\_Title varchar(20) NULL,Ads\_Description varchar(40) NULL,Ads\_Image varchar(50) NULL,主键(Ads\_id),KEY Adv\_id (Adv\_id\_id),KEY Company\_id (Company\_id),约束Adv\_id外键(Adv\_id\_id)在更新级联的删除级联上引用advertiser (Adv\_id),在更新级联上引用company\_information (Company\_id),在删除级联上引用company\_information(Company\_id)) ENGINE=InnoDB AUTO_INCREMENT=5默认CHARSET=latin1;ads\_type ( Ads\_type\_id int(11) NULL AUTO_INCREMENT,Advertisement\_id int(11) NULL,Full\_Channel\_Ads varchar(30) NULL,Logo\_Ads varchar(30) NULL,Com\_Break\_Ads varchar(30) NULL,主键(Ads\_type\_id),密钥Advertisement\_id (Advertisement\_id),约束Advertisement\_id外键(<Ads\_type\_id>D55)引用advertisement (Ads\_id)对删除级联的ENGINE=InnoDB默认CHARSET=latin1;ads\_date ( Ads\_date\_id int(11) NULL AUTO_INCREMENT,Ads\_id int(11) NULL,Starting\_Date varchar(30) NULL,Expiry\_Date varchar(30) NULL,主键(Ads\_date\_id),密钥Ads\_id (Ads\_id),约束Ads\_id外键(Ads\_id)引用advertisement (<Ads\_date\_id>D71)对DELETE级联更新级联的ENGINE=InnoDB默认CHARSET=latin1;我想要寻回
对于上面的行,这个查询工作得很好:
SELECT *
FROM ads_date a
JOIN advertisement ci ON ci.Ads_id = a.Ads_id
JOIN company_information ar ON ar.Company_id = ci.Company_id
JOIN advertiser ad ON ad.Adv_id = ci.Advertiser_id
WHERE Expiry_Date >= CURDATE()问题:我还想从ads_type中检索Advertisement_type。如何使用JOIN查询完成此操作?有人能解释吗?
发布于 2015-08-07 19:27:20
你需要另一个join
SELECT *
FROM ads_date a
JOIN advertisement ci ON ci.Ads_id = a.Ads_id
JOIN company_information ar ON ar.Company_id = ci.Company_id
JOIN advertiser ad ON ad.Adv_id = ci.Advertiser_id
JOIN ads_type at ON at.Advertisement_id = ci.Ads_id -- Here
WHERE Expiry_Date >= CURDATE()发布于 2015-08-07 19:29:00
请尝试以下查询:
SELECT *
FROM ads_date a
JOIN advertisement ci ON ci.Ads_id = a.Ads_id
JOIN company_information ar ON ar.Company_id = ci.Company_id
JOIN advertiser ad ON ad.Adv_id = ci.Advertiser_id
JOIN ads_type at ON at.Advertisement_id =ci.Ads_id
WHERE Expiry_Date >= CURDATE()发布于 2019-04-29 12:23:17
添加另一个连接作为
JOIN ads_type at ON at.Advertisement_id = ci.Ads_id然后使用group_by at.Advertisement_id来避免重复。
https://stackoverflow.com/questions/31885185
复制相似问题