简体   繁体   English

MYSQL 和 PHP 中的 JOIN 查询

[英]JOIN Query In MYSQL and PHP

I am going to generate specific information from all the tables in MYSQL.我将从 MYSQL 中的所有表中生成特定信息。 I have 5 tables in my database.我的数据库中有 5 个表。 Ie: IE:

  1. advertiser广告商

    CREATE TABLE `advertiser` ( `Adv_id` int(11) NOT NULL AUTO_INCREMENT, `Name` char(20) NOT NULL, `F_Name` char(20) NOT NULL, `Address` varchar(40) NOT NULL, `CNIC` int(13) NOT NULL, `Contact` int(11) NOT NULL, `Monthly_fee` varchar(10) NOT NULL, `Region` varchar(10) NOT NULL, `Reg_date` varchar(10) NOT NULL, PRIMARY KEY (`Adv_id`) ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
  2. company_information公司信息

    CREATE TABLE `company_information` ( `Company_id` int(11) NOT NULL AUTO_INCREMENT, `Company_Name` varchar(20) NOT NULL, `Company_Contact` int(11) NOT NULL, `Company_Address` varchar(30) NOT NULL, PRIMARY KEY (`Company_id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  3. advertisement广告

    CREATE TABLE `advertisement` ( `Ads_id` int(11) NOT NULL AUTO_INCREMENT, `Adv_id_id` int(11) NOT NULL, `Company_id` int(11) NOT NULL, `Ads_Title` varchar(20) NOT NULL, `Ads_Description` varchar(40) NOT NULL, `Ads_Image` varchar(50) NOT NULL, PRIMARY KEY (`Ads_id`), KEY `Adv_id` (`Adv_id_id`), KEY `Company_id` (`Company_id`), CONSTRAINT `Adv_id` FOREIGN KEY (`Adv_id_id`) REFERENCES `advertiser` (`Adv_id`) ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT `advertisement_ibfk_1` FOREIGN KEY (`Company_id`) REFERENCES `company_information` (`Company_id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
  4. ads_type广告类型

    CREATE TABLE `ads_type` ( `Ads_type_id` int(11) NOT NULL AUTO_INCREMENT, `Advertisement_id` int(11) NOT NULL, `Full_Channel_Ads` varchar(30) NOT NULL, `Logo_Ads` varchar(30) NOT NULL, `Com_Break_Ads` varchar(30) NOT NULL, PRIMARY KEY (`Ads_type_id`), KEY `Advertisement_id` (`Advertisement_id`), CONSTRAINT `Advertisement_id` FOREIGN KEY (`Advertisement_id`) REFERENCES `advertisement` (`Ads_id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  5. ads_date广告日期

    CREATE TABLE `ads_date` ( `Ads_date_id` int(11) NOT NULL AUTO_INCREMENT, `Ads_id` int(11) NOT NULL, `Starting_Date` varchar(30) NOT NULL, `Expiry_Date` varchar(30) NOT NULL, PRIMARY KEY (`Ads_date_id`), KEY `Ads_id` (`Ads_id`), CONSTRAINT `Ads_id` FOREIGN KEY (`Ads_id`) REFERENCES `advertisement` (`Ads_id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I want to retrive我想找回

  • Name, CNIN, and Contact from table Advertiser来自表 Advertiser 的名称、CNIN 和联系人
  • Company_Name from company_information来自 company_information 的 Company_Name
  • Ads_Title from Advertisement Ads_Title 来自广告
  • Starting_Date and Expiry_Date From ads_date. Starting_Date 和 Expiry_Date 来自 ads_date。

This Query work well for the above rows :此查询适用于上述行:

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()

Problem: I also want to retrive Advertisement_type from ads_type.问题:我还想从 ads_type 中检索 Advertisement_type。 how can I do this with a JOIN query?如何使用JOIN查询执行此操作? Can any one explain?谁能解释一下?

You'll need another join :你需要另一个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()

Please try the below query:请尝试以下查询:

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()

Add another join as添加另一个连接作为

JOIN   ads_type at ON  at.Advertisement_id = ci.Ads_id

Then use group_by at.Advertisement_id to avoid repetition.然后使用group_by at.Advertisement_id避免重复。

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

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