【问题标题】:MySQL-Query with IN, is this result possible?MySQL-Query with IN,这个结果可能吗?
【发布时间】:2012-10-18 05:12:57
【问题描述】:
SELECT      ntc.newsID, ntc.categoryID, category.title
FROM        news_to_category ntc
LEFT JOIN   news_category category
ON          ntc.categoryID = category.categoryID
WHERE       ntc.newsID IN (2,4)

在这张桌子上

news_to_category
    categoryID newsID
    32         2
    33         2
    23         4

news_category
    categoryID title
    32         Important
    33         Cars
    23         Fishing

结果应该是:

Array
(
    [2] => Array
        (
            [0] => Array
                (
                    [0] => 32
                    [1] => Important
                )
            [1] => Array
                (
                    [0] => 33
                    [1] => Cars
                )
        )
    [4] => Array
        (
            [0] => Array
                (
                    [0] => 23
                    [1] => Fishing
                )
        )
)

第二个数组的键应该是 newsID。只需 1 个查询就可以得到这个结果吗? 非常感谢!

【问题讨论】:

  • 你可以用php操作结果数组
  • 是的,我知道,但这将如何运作?我不认为sql查询那样正确..
  • 发布预期的结果和你得到的结果。您可以在 php 中操作排列。

标签: php mysql sql join


【解决方案1】:

查询正确,但会返回一维数组。

【讨论】:

    【解决方案2】:

    像这样工作(表名略有不同):

     SELECT     ctn.newsID, ctn.categoryID, cat.title
        FROM        wcf".WCF_N."_cnews_news_to_category ctn
        LEFT JOIN   wcf".WCF_N."_cnews_category cat
        ON          ctn.categoryID = cat.categoryID
        WHERE       newsID IN (".implode(',',$newsIDs).")
    
        $result = WCF::getDB()->sendQuery($sql);
        $news = array();        
        // save info
        while ($row = WCF::getDB()->fetchArray($result)) {
            $news[$row['newsID']][] = $row;
        }
    

    【讨论】:

      【解决方案3】:

      如果你使用的是 php

      <?php
      
      while($res=mysql_fectch_assoc($resource))
      {
      $result[$res['newsID']][]=array($res['categoryID '],$res['title']);
      
      }
      
      
      
      ?>
      

      【讨论】:

        【解决方案4】:

        试试这个

        $res = mysql_query("SELECT      ntc.newsID, ntc.categoryID, category.title
                            FROM        news_to_category ntc
                            LEFT JOIN   news_category category
                            ON          ntc.categoryID = category.categoryID
                            WHERE       ntc.newsID IN (2,4)");
        
        $array = array();
        
        while($row = mysql_fetch_array($res))
        {
            if(!isset($array[$row["newsID"]]))
            $array[$row["newsID"]]=array();
        
            array_push($array[$row["newsID"]], array($row["categoryID"],$row["title"]));
        }
        
        echo "<pre>";
        var_dump($array);
        

        【讨论】:

          猜你喜欢
          • 2011-02-10
          • 2021-06-29
          • 1970-01-01
          • 2012-09-13
          • 1970-01-01
          • 1970-01-01
          • 2018-07-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多