[英]SQl query to select relational data
I am trying to write the SQL that selects all products
and available features
. 我正在尝试编写选择所有products
和可用features
的SQL。
My database is as follows: 我的数据库如下:
CREATE TABLE IF NOT EXISTS `products` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`product_name` varchar(255) NOT NULL,
`product_description` varchar(255) NOT NULL,
`product_weight` varchar(255) NOT NULL,
`product_price` decimal(11,2) NOT NULL,
`product_image` varchar(255) NOT NULL,
PRIMARY KEY (`product_id`)
);
CREATE TABLE IF NOT EXISTS `features` (
`feature_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`feature_uri` varchar(255) NOT NULL,
`feature_name` varchar(100) NOT NULL,
`feature_title` varchar(150) DEFAULT NULL,
`feature_body` text,
`feature_body_short` varchar(255) DEFAULT NULL,
`feature_image` varchar(255) DEFAULT NULL,
`parent_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`feature_id`),
UNIQUE KEY `feature_uri_UNIQUE` (`feature_uri`),
KEY `parentFK` (`feature_id`),
FULLTEXT KEY `feature_name_FT` (`feature_name`),
FULLTEXT KEY `feature_body_FT` (`feature_body`)
);
CREATE TABLE IF NOT EXISTS `feature_products` (
`feature_product_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`feature_product_order` smallint(6) DEFAULT NULL,
`feature_product_standard` tinyint(1) NOT NULL,
`feature_id` int(11) unsigned NOT NULL,
`product_id` int(11) unsigned NOT NULL,
PRIMARY KEY (`feature_product_id`),
KEY `productFK` (`product_id`),
KEY `featureFK` (`feature_id`)
);
I would like to be able to do this in one loop eg: 我希望能够在一个循环中做到这一点,例如:
{foreach}
<tr>
<td>{name}</td>
<td>{weight}</td>
<td>{if product_id == 1}yes{/if}</td>
<td>{if product_id == 2}yes{/if}</td>
etc
<tr>
{/foreach}
I am using Zend, if this can be of use. 如果可以使用,我正在使用Zend。
Trying to achieve this HTML: 尝试实现此HTML:
UPDATE: 更新:
Non of the suggested answers are working, instead I managed to hack it to work like this: 没有建议的答案有效,相反,我设法破解了它,使其工作如下:
SELECT
p.*,
(
SELECT
GROUP_CONCAT(f.feature_id SEPARATOR ', ')
FROM feature_products fp
LEFT JOIN features f ON f.features_id = fp.feature_id
WHERE fp.product_id = p.product_id
LIMIT 1
) as features
FROM product p
Although the problem with the above code is that it does not return 'feature_product_standard' 尽管上述代码的问题在于它不返回'feature_product_standard'
I'm assuming that you have MySQL, dunno about Zend, but this should get you started. 我假设您有MySQL,对Zend不满意,但这应该可以帮助您入门。
SELECT
p.product_name
, p.product_description
, p.product_weight
, p.product_price
, p.product_image
, GROUP_CONCAT(f.feature_id ORDER BY f.feature_id) as feature_ids
, GROUP_CONCAT(f.feature_name ORDER BY f.feature_id) as feature_names
FROM products p
LEFT JOIN feature_products fp ON (fp.product_id = p.product_id)
LEFT JOIN features f ON (f.feature_id = fp.feature_id)
GROUP BY p.product_id
Not very good code i know, but i hope it helps. 我知道的代码不是很好,但是希望对您有所帮助。 This is using Johan's SQL code. 这是使用Johan的SQL代码。
I haven't tested this. 我还没有测试。
$connection = odbc_connect("Connection info here");
$query = "SELECT
p.product_name
, p.product_description
, p.product_weight
, p.product_price
, p.product_image
, GROUP_CONCAT(f.feature_id ORDER BY f.feature_id) as feature_ids
, GROUP_CONCAT(f.feature_name ORDER BY f.feature_id) as feature_names
FROM products p
LEFT JOIN feature_products fp ON (fp.product_id = p.product_id)
LEFT JOIN features f ON (f.feature_id = fp.feature_id)";
$result = odbc_exec($connection, $query);
while ($data[] = odbc_fetch_array($result));
odbc_close($connection);
if ($data[0]["product_name"]) {
echo "<table><tr>";
foreach (array_keys($data[0]) as $a) { $message .= "<th>".$a."</th>"; }
foreach ($data as $d) {
if ($d["product_name"]) {
if ($d) {
echo "</tr><tr>\n";
foreach ($d as $s) {
echo "<td>".$s."</td>";
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.