[英]MySQL duplicates data in SELECT from multiple tables
I have 6 tables, according to following structure 我有6张桌子,根据以下结构
the query i am using
我正在使用的查询
SELECT
campaigns.idCampaign,
users.idUser AS idUser,
users.identification AS userIdentification,
users.name AS userName,
campaign_users.commission AS userCommission,
campaign_files.idFile AS idFile,
campaign_files.name AS fileName,
clients.identification AS clientIdentification,
suppliers.identification AS supplierIdentification
FROM
campaigns
LEFT JOIN campaign_users ON( campaigns.idCampaign = campaign_users.idCampaign)
LEFT JOIN users ON( users.idUser = campaign_users.idUser )
LEFT JOIN clients USING( idClient )
LEFT JOIN suppliers ON ( suppliers.idSupplier = campaigns.idSupplier )
LEFT JOIN campaign_files ON( campaigns.idCampaign = campaign_files.idCampaign)
This results in duplicate campaigns, according to the number of files in campaign_files
or users in campaign_users
(which is greater). 这导致在重复的广告活动,根据文件数
campaign_files
或用户campaign_users
(这是更大)。
this is the result, as you can see the
idCampaign
is same but multiple times, i want to this in single object, like this 这是结果,因为您可以看到
idCampaign
相同但多次,我想在单个对象中这样做,就像这样
{
idCampaign: 4,
users: [
{
idUser: 1,
userName: 'ADMIN'
},
{
idUser: 2,
userName: 'Serena Huel'
}
],
files: [
{
idFile: 23,
fileName: 'dshds9agds86das8gads8g5dsa.hal'
},
{
idFile: 49,
fileName: 'dshds9agds86das8gads8g5dsa.hal'
}
{
idFile: 84,
fileName: 'dshds9agds86das8gads8g5dsa.hal'
},
{
idFile: 99,
fileName: 'dshds9agds86das8gads8g5dsa.hal'
}
],
clientIdentification: "dolore",
...
}
I have added the output as example but it can be PHP object, array etc. 我已经添加了输出作为示例,但是它可以是PHP对象,数组等。
Sadly, this is not how MySQL works. 可悲的是,这不是MySQL的工作方式。 You could do a
GROUP BY
and something like GROUP_CONCAT
, but this would leave you with strings and not arrays. 您可以执行
GROUP BY
,例如GROUP_CONCAT
,但这将使您拥有字符串而不是数组。 So why not turn it into the wanted object in PHP? 那么为什么不将其转换为PHP中的通缉对象呢?
Assuming the query can return multiple campaignId
s, you could do it like this 假设查询可以返回多个
campaignId
,则可以这样进行
$campaigns = array();
while ($row = /* fetch row */) {
if (!isSet($campaigns[ $row['campaignId'] ])) {
//new campaignId
$campaigns[ $row['campaignId'] ] = array(
'users' => array(),
'files' => array(),
'clientIdentification' => $row['clientIdentification']
);
}
if (!isSet($campaigns[ $row['campaignId'] ]['users'][ $row['idUser'] ])) {
//new user
$campaigns[ $row['campaignId'] ]['users'][ $row['idUser'] ] = $row['userName'];
}
$campaigns[ $row['campaignId'] ]['files'][] = array(
'idFile' => $row['idFile'],
'fileName' => $row['fileName']
);
}
This will give you an array $campaigns
which is nearly what you want. 这将为您提供一个数组
$campaigns
,几乎是您想要的。 Saving the users using the id as key is a simple method to not have any duplicate users. 使用id作为密钥保存用户是一种简单的方法,不会有任何重复的用户。 Now to get the expected object, you can do something like
现在要获得预期的对象,您可以执行以下操作
foreach ($campaigns as $c) {
$expectedObj = array(
'idCampaign' => $c['idCampaign'],
'users' => array(),
'files' => $c['files'],
'clientIdentification' => $c['clientIdentification']
);
foreach ($c['users'] as $idUser => $userName) {
$expectedObj['users'][] = array(
'idUser' => $idUser,
'userName' => $userName
);
}
// use $expectedObj
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.