簡體   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