我有3个TABLES产品,products_extra_fields,products_to_products_extra_fields
产品表
+-------------+--------------+---------------+
| products_id | categories_id| products_name |
+-------------+--------------+---------------+
| 1 | 1 | product 1 |
| 2 | 1 | product 2 |
+-------------+--------------+---------------+
products_extra_fields TABLE
+--------------------------+---------------------------+
| products_extra_fields_id | products_extra_fields_name|
+--------------------------+---------------------------+
| 1 | sugar |
| 2 | cocoa |
| 3 | butter |
| 4 | milk |
+--------------------------+---------------------------+
products_to_products_extra_fields TABLE
+-------------+---------------------------+-----------------------------+
| products_id | products_extra_fields_id | products_extra_fields_value |
+-------------+---------------------------+-----------------------------+
| 1 | 1 | 20% |
| 1 | 2 | 45% |
| 1 | 3 | 27% |
| 2 | 1 | 12% |
+-------------+---------------------------+-----------------------------+
我想编辑产品1 我使用此查询:
SELECT
products_name,
products_extra_fields_name,
products_extra_fields_value FROM products, products_extra_fields, products_to_products_extra_fields
WHERE products_to_products_extra_fields.products_extra_fields_id = products_extra_fields.products_extra_fields_id AND
products_to_products_extra_fields.products_id=1
GROUP BY products_extra_fields.products_extra_fields_id;
+---------------+---------------------------+----------------------------+
| products_name | products_extra_fields_name| products_extra_fields_value|
+---------------+---------------------------+----------------------------+
| product 1 | sugar | 20% |
| product 1 | cocoa | 45% |
| product 1 | butter | 27% |
+---------------+---------------------------+----------------------------+
但我想看看
products_extra_fields_name(牛奶)没有价值
+---------------+---------------------------+----------------------------+
| products_name | products_extra_fields_name| products_extra_fields_value|
+---------------+---------------------------+----------------------------+
| product 1 | sugar | 20% |
| product 1 | cocoa | 45% |
| product 1 | butter | 27% |
| product 1 | milk | |
+---------------+---------------------------+----------------------------+
对于产品2可可,黄油,牛奶,没有价值只有糖12%
我可以做些什么来获得我想要的结果。
查看我的php脚本(products_edit2.php?id = 1或2)
<?php
require ('mysql/mysql_connect.php'); // Connect to the database.
if (is_numeric ($_GET['id']) ) {
$query = "SELECT
products_name, products_extra_fields_name, products_extra_fields_value FROM
products, products_extra_fields, products_to_products_extra_fields WHERE
products_to_products_extra_fields.products_extra_fields_id = products_extra_fields.products_extra_fields_id AND
products_to_products_extra_fields.products_id = {$_GET['id']}
GROUP BY products_extra_fields.products_extra_fields_id";
if ($result = mysql_query ($query)) {
$query2 = "SELECT products_name FROM products WHERE products_id={$_GET['id']}";
if ($result2 = mysql_query ($query2)) {
$row2 = mysql_fetch_array ($result2);
$products_name = $row2['products_name'];
?>
<form action='products_edit.php' enctype='multipart/form-data' method='POST'>
<table width="50%" border="0" cellpadding="2" cellspacing="2">
<tr>
<td width="20%" align="right">PRODUCTS NAME:</td>
<td><input type="text" name="products_name" size="80" value="<?php echo $products_name; ?>" /></td>
</tr>
<?php
}
/***** ***** *****/
while ($row = mysql_fetch_assoc ($result)) {
$products_extra_fields_name = $row['products_extra_fields_name'];
$products_extra_fields_value = $row['products_extra_fields_value'];
$products_extra_fields_id = $row['products_extra_fields_id'];
?>
<tr>
<td width="20%" align="right"><?php echo $products_extra_fields_id .'-'. $products_extra_fields_name; ?></td>
<td><input type="text" name="categories_name" size="80" value="<?php echo $products_extra_fields_value; ?>" /></td>
</tr>
<?php }
/***** ***** END *****/
?>
<input type="hidden" name="categories_id" size="10" value="<?php echo $_GET['id']; ?>" />
<tr>
<td width="100%" colspan="2" align="right"><input type="submit" name="submit" value="SAVE"/></td>
</tr>
</table>
</form>
<?php
} else { // Couldn't get the information.
print "<p>Could retrieve the entry because: <b>" . mysql_error() . "</b><br/><br/>. The query was $query.</p>";
}
}else{ // No ID set.
print '<p><b>You must have made a mistake in using this page.</b></p>';
}
?>
</body>
</html>
我想看到所有4个products_extra_fields_name和4个输入框(产品1有3个值1 null,产品2有1个值3 null)
答案 0 :(得分:3)
在FROM中使用逗号分隔列表时,MySQL使用内部联接。要得到你想要的东西,你需要使用左连接。
SELECT
products_name,
products_extra_fields_name,
products_extra_fields_value
FROM products, products_extra_fields
LEFT JOIN products_to_products_extra_fields
ON products_to_products_extra_fields.products_extra_fields_id = products_extra_fields.products_extra_fields_id
AND products_to_products_extra_fields.products_id=1
GROUP BY products_extra_fields.products_extra_fields_id;
这应该产生:
+---------------+---------------------------+----------------------------+
| products_name | products_extra_fields_name| products_extra_fields_value|
+---------------+---------------------------+----------------------------+
| product 1 | sugar | 20% |
| product 1 | cocoa | 45% |
| product 1 | butter | 27% |
| product 1 | milk | NULL |
+---------------+---------------------------+----------------------------+