简体   繁体   中英

How to get multiple columns from the same table

I have 4 $queries concatenated like below:

this select one attribute called 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. 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? 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.

1: One record per product per attribute

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. 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

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  |
+-------------------------------------------------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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