MySQL php从数据库获取项目并以XML形式存储(重复项目)
首先,这里是我的代码,它是有效的,只是有一个主要的缺陷,我不知道如何修复。为了更好地阅读,我已经清除了所有元素的代码,除了那些导致问题的元素MySQL php从数据库获取项目并以XML形式存储(重复项目),php,mysql,xml,Php,Mysql,Xml,首先,这里是我的代码,它是有效的,只是有一个主要的缺陷,我不知道如何修复。为了更好地阅读,我已经清除了所有元素的代码,除了那些导致问题的元素 $servername = "localhost"; $username = DB_USERNAME; $password = DB_PASSWORD; $database = DB_DATABASE; // Start of table variables // End of table variabl
$servername = "localhost";
$username = DB_USERNAME;
$password = DB_PASSWORD;
$database = DB_DATABASE;
// Start of table variables
// End of table variables
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
$sql = "SELECT pd.name as product_name, p.model as product_model, p.quantity as product_quantity, cd.name as category_name , p.product_id as product_id , p.price as product_price , m.name as product_manufacturer , p.image as product_image , pd.description as product_description
FROM product p
INNER JOIN product_description pd ON pd.product_id = p.product_id
INNER JOIN product_to_category ptc ON ptc.product_id = p.product_id
INNER JOIN category_description cd ON cd.category_id = ptc.category_id
INNER JOIN manufacturer m ON m.manufacturer_id = p.manufacturer_id
WHERE pd.language_id = 2 AND cd.language_id = 2 AND p.status = 1 AND p.product_id = p.product_id AND p.manufacturer_id = p.manufacturer_id";
$conn->set_charset('utf8mb4');
$result = $conn->query($sql);
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);
$xml->startElement('products');
if ($result->num_rows > 0) {
while ($product = $result->fetch_assoc()) {
$product_id = $product["product_id"];
$product_category = $product["category_name"];
$xml->startElement("product");
$xml->startElement("itemid");
$xml->writeRaw($product_model);
$xml->endElement();
$xml->startElement("category");
$xml->writeCDATA($product_category);
$xml->endElement();
//end
$xml->endElement();
}
}else{
}
$xml->endElement();
$xml->flush();
?>
当我运行代码时,一切都开始了,它生成了XML结构。我在下面发布的XML是第一个ID为23793的产品
代码复制了我的产品3次,因为每个产品都有一个主类游戏、子类PS3和子类战斗
我怎样才能使它不会重复同一个产品3次,但对于产品23793,可以获得游戏>PS3>战斗等类别
我试着在类别上做一个foreach循环,但我不认为它来自php,但问题来自MySQL选择代码
有什么想法吗?您需要在查询中添加GROUP\u CONCAT
选择
pd.name作为产品名称,
p、 模型作为产品模型,
p、 数量作为产品数量,
组\u CONCATcd.name分隔符“>”作为类别\u名称,
p、 产品标识作为产品标识,
p、 价格作为产品价格,
m、 作为产品制造商的名称,
p、 形象作为产品形象,
产品描述作为产品描述
来自产品p
内部连接产品描述pd上的pd.product\U id=p.product\U id
在ptc.product\U id=p.product\U id上将产品\U内部连接到类别ptc
内部连接类别\u说明cd上的cd.category\u id=ptc.category\u id
内部连接m上的制造商m.manufacturer\u id=p.manufacturer\u id
哪里
pd.language_id=2
和cd.language_id=2
p.status=1
p.product\u id=p.product\u id
p.manufacturer\u id=p.manufacturer\u id
按产品编号分组
试试这个:
select case when@var <> brand then @Var :=p.product_id end as product_id
(
SELECT pd.name as product_name, p.model as product_model, p.quantity as product_quantity, cd.name as category_name , p.product_id as product_id , p.price as product_price , m.name as product_manufacturer , p.image as product_image , pd.description as product_description
FROM product p
INNER JOIN product_description pd ON pd.product_id = p.product_id
INNER JOIN product_to_category ptc ON ptc.product_id = p.product_id
INNER JOIN category_description cd ON cd.category_id = ptc.category_id
INNER JOIN manufacturer m ON m.manufacturer_id = p.manufacturer_id
WHERE pd.language_id = 2 AND cd.language_id = 2 AND p.status = 1 AND p.product_id = p.product_id AND p.manufacturer_id = p.manufacturer_id
GROUP BY p.product_id
)
as foo
cross join (Select @var := '') as foo2
您可以在顶部选择中添加所需的其他字段,但这将基本上消除第一个字段中产品的重复。请您详细解释一下GROUP_CONCAT是什么?in基本上将数据行组合并为单个字符串。可能更容易理解。但是不管怎样,想知道新的查询是否有帮助?你也需要在查询中添加一个p.product_id分组。谢谢@rob-我通过在where子句中选择一个产品id进行了测试,并通过以下方式对分组进行了检查:@LucasKrupinski工作得很有魅力,你是deseve爵士奖牌,太棒了,你能用synax解释这个分组吗?
product_id | category_id
23793 | 58 (This is the ID of Games)
23793 | 135 (This is the ID of PS3)
23793 | 777 (This is the ID of Fighting)
select case when@var <> brand then @Var :=p.product_id end as product_id
(
SELECT pd.name as product_name, p.model as product_model, p.quantity as product_quantity, cd.name as category_name , p.product_id as product_id , p.price as product_price , m.name as product_manufacturer , p.image as product_image , pd.description as product_description
FROM product p
INNER JOIN product_description pd ON pd.product_id = p.product_id
INNER JOIN product_to_category ptc ON ptc.product_id = p.product_id
INNER JOIN category_description cd ON cd.category_id = ptc.category_id
INNER JOIN manufacturer m ON m.manufacturer_id = p.manufacturer_id
WHERE pd.language_id = 2 AND cd.language_id = 2 AND p.status = 1 AND p.product_id = p.product_id AND p.manufacturer_id = p.manufacturer_id
GROUP BY p.product_id
)
as foo
cross join (Select @var := '') as foo2