简体   繁体   English

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

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

I have 4 $queries concatenated like below: 我有4个串联的$ queries,如下所示:

this select one attribute called shape: 这将选择一个称为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`";

To select the "clarity" from the same table and same column: 要从同一表和同一列中选择“澄清度”:

$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'"

and there are few other select statement such as this one. 很少有其他select语句,例如这一条。 I tried to merged them together but I couldn't. 我试图将它们合并在一起,但是我不能。 Now I am running multiple queries and hoping to get it work with the following but I am not returning any values: 现在,我正在运行多个查询,并希望将其与以下内容一起使用,但我不返回任何值:

$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;

The variable output is for a bigger function and how I output them on my page. 变量输出用于更大的函数,以及如何在页面上输出它们。 Please any suggestion on why the mysqli_multi_query() is not working? 请任何关于为什么mysqli_multi_query()无法正常工作的建议? or if I could merge queries like that into one. 或者我是否可以将这样的查询合并为一个。 All helps are appreciated in advance. 所有帮助都将受到赞赏。

If you are just fetching separate attributes in different queries then you can achieve that by modifying ON clause in the join. 如果只是在不同的查询中获取单独的属性,则可以通过修改联接中的ON子句来实现。

1: One record per product per attribute 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          |
+-----------------------------------------------------------------------------------+

Note: If you have ORDER BY or GROUP BY for a particular attribute only, it won't work here. 注意:如果仅对特定属性具有ORDER BYGROUP BY ,则在这里将无法使用。 If you apply it will be applicable for all attributes which you have included in this join. 如果应用,它将适用于此联接中包含的所有属性。

2: One record per product, all attributes as columns 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