我有4个表,我想使用一个查询从这些表中选择信息。
我有一个表名
tbl_marketing
db_maid db_client db_status db_process
4 test done ddd
tbl_meeting
db_meetingid db_meetingsubject db_mid
1 test 4
tbl_phonecall
db_phid db_subject db_mid
1 ggg 4
2 fff 4
tbl_email
db_eid db_email db_mid
1 xxx 4
备注:这是我的4个表的示例。我的表包含更多列
db_mid
是tbl_marketing
我想在不重复的情况下选择所有信息
我使用此查询:
select
marketing.*,
meeting.db_meetingid,meeting.db_meetingsubject,meeting.db_sd,meeting.db_dd,meeting.db_duration as meetingDuration,meeting.db_place,meeting.db_mom,meeting.db_momattache,meeting.db_status as meetingStatus,meeting.db_nextmeeting,meeting.db_lastmeeting,meeting.db_attendees,meeting.db_note as meetingNote,meeting.db_mid,
phonecall.db_phid,phonecall.db_subject as phoneSubject,phonecall.db_desc as phoneDesc,phonecall.db_nextdate,phonecall.db_doc,phonecall.db_duration as phoneDuration,phonecall.db_phstatus,phonecall.db_pnote,phonecall.db_client as phoneClient,phonecall.db_crf,phonecall.db_callto,phonecall.db_phone as phonecallPhone,phonecall.db_logs as phoneLogs,phonecall.db_mid,phonecall.db_dateedit as phoneEdit,phonecall.db_phdate,
email.db_eid,email.db_edate,email.db_esubject,email.db_edesc,email.db_erf,email.db_emailto,email.db_email as eEmail,email.db_dos,email.db_dor,email.db_estatus,email.db_logs as eLogs,email.db_note as eNote,
location.db_location as location,
location.db_lid,
company.db_company as com,
company.db_coid,
subcompany.db_subid,
subcompany.db_subcompany as sub,
user.db_uid,
concat(user.db_fname,' ' ,user.db_lname) as tr,
subj.db_subjid,
subj.db_subject as s
from tbl_marketing as marketing
left join tbl_location as location
on
marketing.db_location=location.db_lid
left join tbl_meeting as meeting
on
marketing.db_maid=meeting.db_mid
left join tbl_phonecall as phonecall
on
marketing.db_maid=phonecall.db_mid
left join tbl_email as email
on
marketing.db_maid=email.db_mid
left join tbl_company as company
on
marketing.db_companyname=company.db_coid
left join tbl_subcompany as subcompany
on
marketing.db_subcompany=subcompany.db_subid
left join tbl_user as user
on
marketing.db_transfered=user.db_uid
left join tbl_subject as subj
on
phonecall.db_subject=subj.db_subjid
where
marketing.db_maid='$read'
and
meeting.db_mid='$read'
and
email.db_mid='$read'
and
phonecall.db_mid='$read'
我使用此查询来选择信息,但当我有两行时,示例中的db_mid
与tbl_phonecall
相同,则信息重复如下:
表格营销
1 test 4
1 test 4
表电话
1 ggg 4
2 fff 4
表格会议
1 test 4
1 test 4
表格电子邮件
1 xxx 4
1 xxx 4
如上所见,所有表都有重复的值没有phonecall,因为他有2行我想显示没有重复的所有信息
如果我在中途使用群组,我将无法收到tbl_phonecall
$rows=array();
while($rows=mysqli_fetch_array($sql)){
$row[]=$rows;}
<table>
foreach($rows as row){
<tr>
<td><?php echo $row['phID'];?></td>
</tr>
}
</table>
输出
# subject mid
1 test 4
2 ggg 4
不喜欢这个
1,2测试,ggg 4,4
答案 0 :(得分:0)
如果您在phonecall表中只有重复项,则以下内容应该有效。如果其他左连接表可以有重复项,只需根据需要添加GROUP_CONCAT。
select
marketing.*,
meeting.db_meetingid,
meeting.db_meetingsubject,
meeting.db_sd,
meeting.db_dd,
meeting.db_duration as meetingDuration,
meeting.db_place,
meeting.db_mom,
meeting.db_momattache,
meeting.db_status as meetingStatus,
meeting.db_nextmeeting,
meeting.db_lastmeeting,
meeting.db_attendees,
meeting.db_note as meetingNote,
meeting.db_mid,
GROUP_CONCAT(phonecall.db_phid) as phID,
GROUP_CONCAT(phonecall.db_subject) as phoneSubject,
GROUP_CONCAT(phonecall.db_desc) as phoneDesc,
GROUP_CONCAT(phonecall.db_nextdate) as phNextDate,
GROUP_CONCAT(phonecall.db_doc) as phDoc,
GROUP_CONCAT(phonecall.db_duration) as phoneDuration,
GROUP_CONCAT(phonecall.db_phstatus) as phStatus,
GROUP_CONCAT(phonecall.db_pnote) as phPNote,
GROUP_CONCAT(phonecall.db_client) as phoneClient,
GROUP_CONCAT(phonecall.db_crf) as phCrf,
GROUP_CONCAT(phonecall.db_callto) as phCallTo,
GROUP_CONCAT(phonecall.db_phone) as phonecallPhone,
GROUP_CONCAT(phonecall.db_logs) as phoneLogs,
GROUP_CONCAT(phonecall.db_mid) as phMid,
GROUP_CONCAT(phonecall.db_dateedit) as phoneEdit,
GROUP_CONCAT(phonecall.db_phdate) as phdate,
email.db_eid,
email.db_edate,
email.db_esubject,
email.db_edesc,
email.db_erf,
email.db_emailto,
email.db_email as eEmail,
email.db_dos,
email.db_dor,
email.db_estatus,
email.db_logs as eLogs,
email.db_note as eNote,
location.db_location as location,
location.db_lid,
company.db_company as com,
company.db_coid,
subcompany.db_subid,
subcompany.db_subcompany as sub,
user.db_uid,
concat(user.db_fname,' ' ,user.db_lname) as tr,
subj.db_subjid,
subj.db_subject as s
from tbl_marketing as marketing
left join tbl_location as location
on marketing.db_location=location.db_lid
left join tbl_meeting as meeting
on marketing.db_maid=meeting.db_mid
left join tbl_phonecall as phonecall
on marketing.db_maid=phonecall.db_mid
left join tbl_email as email
on marketing.db_maid=email.db_mid
left join tbl_company as company
on marketing.db_companyname=company.db_coid
left join tbl_subcompany as subcompany
on marketing.db_subcompany=subcompany.db_subid
left join tbl_user as user
on marketing.db_transfered=user.db_uid
left join tbl_subject as subj
on phonecall.db_subject=subj.db_subjid
where marketing.db_maid='$read' and
meeting.db_mid='$read' and
email.db_mid='$read' and
phonecall.db_mid='$read'
GROUP BY meeting.db_mid