繁体   English   中英

如何从同一个表中获取多个列

[英]How to get multiple columns from the same table

我有4个串联的$ queries,如下所示:

这将选择一个称为shape的属性:

    $sql = "
SELECT p.`ID` AS 'Product ID', 
       p.`post_title` AS 'Product Name', 
       t.`term_id` AS 'Attribute Value ID', 
       REPLACE(REPLACE(tt.`taxonomy`, 'pa_', ''), '-', ' ') AS 'Attribute Name', 
       t.`name` AS 'Shape' 
       FROM `wp_posts` AS p 
       INNER JOIN `wp_term_relationships` AS tr ON p.`ID` = tr.`object_id` 
       INNER JOIN `wp_term_taxonomy` AS tt ON tr.`term_taxonomy_id` = tt.`term_id` AND tt.`taxonomy` LIKE 'pa_shape%'
       INNER JOIN `wp_terms` AS t ON tr.`term_taxonomy_id` = t.`term_id` 
       WHERE p.`post_type` = 'product' 
       AND p.`post_status` = 'publish' ORDER BY p.`ID`";

要从同一表和同一列中选择“澄清度”:

$sql .= "SELECT p.`ID` AS 'Product ID', 
       p.`post_title` AS 'Product Name', 
       t.`term_id` AS 'Attribute Value ID', 
       REPLACE(REPLACE(tt.`taxonomy`, 'pa_', ''), '-', ' ') AS 'Attribute Name', 
       t.`name` AS 'Clarity' 
       FROM `wp_posts` AS p 
       INNER JOIN `wp_term_relationships` AS tr ON p.`ID` = tr.`object_id` 
       INNER JOIN `wp_term_taxonomy` AS tt ON tr.`term_taxonomy_id` = tt.`term_id` AND tt.`taxonomy` LIKE 'pa_clarity%'
       INNER JOIN `wp_terms` AS t ON tr.`term_taxonomy_id` = t.`term_id` 
       WHERE p.`post_type` = 'product'"

很少有其他select语句,例如这一条。 我试图将它们合并在一起,但是我不能。 现在,我正在运行多个查询,并希望将其与以下内容一起使用,但我不返回任何值:

$output .= "<table><thead><tr><th>Carat</th><th>Color</th><th>Cut</th><th>Shape</th><th>Clarity</th></tr></thead><tbody>";


if (mysqli_multi_query($con,$sql))
{
  do
    {
    // Store first result set
    if ($result=mysqli_store_result($con)) {
      // Fetch one and one row
      while ($row=mysqli_fetch_row($result))
        {
        $output .= "<td>" . $row['Carat'] . "</td><td>" . $row['Color'] . "</td><td>" . $row['Cut'] . "</td><td>" . $row['Shape'] . "</td><td>" . $row['Clarity'] . "</td></tr>" ;
                }
              // Free result set
              mysqli_free_result($result);
              }
            }
          while (mysqli_next_result($con));
        }

        mysqli_close($con);
        $output .= "</table>";



        return $output;

变量输出用于更大的函数,以及如何在页面上输出它们。 请任何关于为什么mysqli_multi_query()无法正常工作的建议? 或者我是否可以将这样的查询合并为一个。 所有帮助都将受到赞赏。

如果只是在不同的查询中获取单独的属性,则可以通过修改联接中的ON子句来实现。

1:每个属性每个产品一个记录

SELECT 
    p.`ID` AS 'Product ID', 
    p.`post_title` AS 'Product Name', 
    t.`term_id` AS 'Attribute Value ID', 
    REPLACE(REPLACE(tt.`taxonomy`, 'pa_', ''), '-', ' ') AS 'Attribute Name', 
    t.`name` AS 'Attribute Value' 
 FROM `wp_posts` AS p 
 INNER JOIN `wp_term_relationships` AS tr ON p.`ID` = tr.`object_id` 
 INNER JOIN `wp_term_taxonomy` AS tt ON tr.`term_taxonomy_id` = tt.`term_id` 
    AND (tt.`taxonomy` LIKE 'pa_clarity%' OR tt.`taxonomy` LIKE 'pa_shape%') -- If require add more attribute here and you are good
 INNER JOIN `wp_terms` AS t ON tr.`term_taxonomy_id` = t.`term_id` 
 WHERE p.`post_type` = 'product';

Output:
+-----------------------------------------------------------------------------------+
| Product ID | Product Name | Attribute Value ID | Attribute Name | Attribute Value |
| p1         | p1_name      | 10                 | Clarity        | Not clear       |
| p1         | p1_name      | 11                 | Shape          | Square          |
| p2         | p2_name      | 10                 | Clarity        | Clear           |
| p2         | p2_name      | 11                 | Shape          | Circle          |
+-----------------------------------------------------------------------------------+

注意:如果仅对特定属性具有ORDER BYGROUP BY ,则在这里将无法使用。 如果应用,它将适用于此联接中包含的所有属性。

2: 每个产品一条记录,所有属性作为列

SELECT 
    p.`ID` AS 'Product ID', 
    p.`post_title` AS 'Product Name', 
    GROUP_CONCAT(IF(tt.`taxonomy` LIKE 'pa_clarity%', t.`name`, NULL)) AS 'Clarity',
    GROUP_CONCAT(IF(tt.`taxonomy` LIKE 'pa_shape%', t.`name`, NULL)) AS 'Shape' -- Add more attributes in same way
 FROM `wp_posts` AS p 
 INNER JOIN `wp_term_relationships` AS tr ON p.`ID` = tr.`object_id` 
 INNER JOIN `wp_term_taxonomy` AS tt ON tr.`term_taxonomy_id` = tt.`term_id` 
    AND (tt.`taxonomy` LIKE 'pa_clarity%' OR tt.`taxonomy` LIKE 'pa_shape%') -- If require add more attribute here and you are good
 INNER JOIN `wp_terms` AS t ON tr.`term_taxonomy_id` = t.`term_id` 
 WHERE p.`post_type` = 'product'
 GROUP BY p.`ID`;

Output:
+-------------------------------------------------+
| Product ID | Product Name | Clarity   | Shape   |
| p1         | p1_name      | Not clear | Circle  |
| p2         | p2_name      | clear     | Square  |
+-------------------------------------------------+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM