Php 如何在msql中连接四个表?

Php 如何在msql中连接四个表?,php,Php,我想用mysql连接从四个表中选择数据。我正在使用 create table area ( area_id int(11) not null auto_increment, area_name varchar(60) not null, primary key (area_id) ); create table mainCategory( mc_id int(11) not null auto_increment, mc_name varchar(60) not null, area_id i

我想用mysql连接从四个表中选择数据。我正在使用

create table area (
area_id int(11) not null auto_increment,
area_name varchar(60) not null,
primary key (area_id)
);

create table mainCategory(
mc_id int(11) not null auto_increment,
mc_name varchar(60) not null,
area_id int(11) not null,
primary key(mc_id),
foreign key(area_id) references area(area_id)
);

create table subCategory(
sc_id int(11) not null auto_increment,
sc_name varchar(60) not null,
mc_id int(11) not null,
area_id int(11) not null,
primary key(sc_id),
foreign key(mc_id) references mc(mc_id),
foreign key(area_id) references area(area_id)
);


create table shopes(
s_id int(11) not null auto_increment,
s_name varchar(60) not null,
s_address varchar(120) not null,
s_work varchar(255) not null,
s_imagepath varchar(255) not null,
s_image varchar(255) not null,
area_id int(11) not null,
mc_id int(11) not null,
sc_id int(11) not null,
primary key(s_id),
foreign key(area_id) references area(area_id),
foreign key(mc_id) references mc(mc_id),
foreign key(sc_id) references sc(sc_id)
);
它是一个三表联接,没有给出适当的结果。它给出了重复的结果。

使用此语法

select s_name,s_address,s_work,s_image,area_name,mc_name from shopes inner join area on area.area_id=shopes.area_id inner join mainCategory on mc.area_id=area.area_id;
如果仍有重复数据,请使用:

select s_name, s_address, s_work, s_image, area_name, mc_name
from shopes, area, mainCategory
where shopes.area_id = area.area_id
and shopes.mc_id = mainCategory.mc_id;
试试这个:

select distinct s_name, ...
我使用了LEFT JOIN关键字而不是internal JOIN,它返回左表(表1)中的所有行,右表(表2)中有匹配的行。当没有匹配项时,右侧的结果为空。

试试看。
select s.s_name, s.s_address, s.s_work, s.s_image, a.area_name, mc.mc_name  from shope as s LEFT JOIN area as a on a.area_id=s.area_id LEFT JOIN mainCategory as mc on mc.area_id=a.area_id LEFT JOIN subCategory as sc on sc.area_id=a.area_id
从shopes上的shopes join area中选择shopes.s_name、s_address、s_work、s_image、area.area_name、maincategory.mc_name。area_id=area.area_id
在shopes.mc_id=maincography.mc_id上加入maincography

您是否尝试过:
从区域上的shopes内部连接区域中选择s\u名称、s\u地址、s\u工作、s\u图像、区域\u名称、mc\u名称。区域\u id=shopes.area\u id内部连接主类别在mc上。区域\u id=区域.area\u id按shopes.id分组
select s.s_name, s.s_address, s.s_work, s.s_image, a.area_name, mc.mc_name  from shope as s LEFT JOIN area as a on a.area_id=s.area_id LEFT JOIN mainCategory as mc on mc.area_id=a.area_id LEFT JOIN subCategory as sc on sc.area_id=a.area_id
select s_name,s_address,s_work,s_image,area_name,mc_name
  from area
 inner join area
    on area.area_id=shopes.area_id
 inner join mainCategory
    on mc.area_id=area.area_id;