0
company                    company_to_attributes
=====================      ============================
| id_company | name        id_attributes | id_company
=====================      ============================
| 1          | Test        | 1           | 1             
| 2          | Test 2      | 2           | 1              
| 3          | Test 3      | 3           | 1         
| 4          | Test 4      | 4           | 3
| ..         | ...         | ..          | ... 
=====================      ============================

私は単純な配列を持っています:

$array_attributes_IDS = array(1,2);

配列から id_attribute を持つ会社を取得するクエリを書きたいと思います。

4

6 に答える 6

2

これを試して、

$array_attributes_IDS = array(1,2);
$finalArray = implode(',', $array_attributes_IDS);
$arrCount = count($array_attributes_IDS);

$query ="    SELECT com.name
             FROM   company com
             INNER JOIN company_to_attributes att
                ON com.id_company = att.id_company
             WHERE  att.id_attributes IN ($finalArray)
             GROUP  BY com.name
             HAVING COUNT(*) = $arrCount";

TEST両方の属性が含まれているため、これは 1 つの会社のみを返します。

于 2013-04-09T09:30:03.840 に答える
0

以下を試してください:

    $array_attributes_IDS = array(1,2);
    $finalArray = implode(',', $array_attributes_IDS);
    $query = "SELECT C.name FROM company_to_attributes CA INNER JOIN company C ON CA.id_company = C.id_company WHERE CA.id_attributes IN ($finalArray)";

これが役立つことを願っています。

于 2013-04-09T09:30:54.267 に答える
0
SELECT  c.name
FROM    company c
       JOIN company_to_attributes a
            ON c.id_company = a.id_company
WHERE   a.id_attributes IN (1,2)
于 2013-04-09T09:31:00.740 に答える
0
$sql = "
SELECT t1.name FROM company_to_attributes
LEFT JOIN company AS t1 ON t1.id_company = company_to_attributes.id_company
WHERE company_to_attributes.id_attributes IN (".implode(',', $array_attributes_IDS).")
";
于 2013-04-09T09:31:54.787 に答える
0
SELECT c.* FROM company c INNER JOIN company_to_attributes ca ON c.id_company=ca.id_company WHERE ca.id_attributes IN (1,2)
于 2013-04-09T09:29:58.910 に答える