在MYSQL和PHP中加入查询

我将从MYSQL中的所有表中生成特定的信息。 我的数据库中有5个表格。 即:

  • 广告

    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;

  • 公司信息

    CREATE TABLE company_informationCompany_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 ;

  • 广告

    CREATE TABLE advertisementAds_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_idAdv_id_id ),KEY Company_idCompany_id ),约束Adv_id外键( Adv_id_id )参考advertiserAdv_id )ON DELETE CASCADE ON UPDATE CASCADE,约束advertisement_ibfk_1外键( Company_id )参考company_informationCompany_id )ON DELETE CASCADE ON UPDATE CASCADE)ENGINE = InnoDB AUTO_INCREMENT = 5 DEFAULT CHARSET = latin1;

  • ads_type

    CREATE TABLE ads_typeAds_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_idAdvertisement_id ),约束Advertisement_id外键( Advertisement_id )参考advertisementAds_id )ON DELETE CASCADE ON UPDATE CASCADE)ENGINE = InnoDB的默认字符集= LATIN1;

  • ads_date

    CREATE TABLE ads_dateAds_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_idAds_id ),CONSTRAINT Ads_id FOREIGN KEY( Ads_id )REFERENCES advertisementAds_id )ON DELETE CASCADE ON UPDATE CASCADE)ENGINE = InnoDB DEFAULT CHARSET = latin1;

  • 我想回顾一下

  • 名称,CNIN和来自桌面广告商的联系人
  • 来自company_information的Company_Name
  • 来自广告的Ads_Title
  • Starting_Date和Expiry_Date来自ads_date。
  • 此查询适用于上述行:

    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查询来做到这一点? 任何人都可以解释吗?


    你需要另一个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()
    

    请尝试以下查询:

    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()
    
    链接地址: http://www.djcxy.com/p/59701.html

    上一篇: JOIN Query In MYSQL and PHP

    下一篇: how to fetch menu from database with its subcategory