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

I have a simple array:

$array_attributes_IDS = array(1,2);

I want write a query for get the companies that have id_attribute from array.

1
  • Please present some examples of what you want to do and what the result should be. Commented Apr 9, 2013 at 9:29

6 Answers 6

2

Try this,

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

this will return only one company TEST because contains both attributes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thk for the reply. The query works good, but my array is dynimical, i don't how many elements contains. How resolve for the query COUNT ?
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)

Comments

0

please try the following:

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

Hope this would help.

2 Comments

Thk for the answer. I would select all companies that have all value of array_attributes_IDS. If have all value.
@Mike: with INNER JOIN you will get all Companies which are associated with atleast 1 attribute. I hope this is what you want. Or do you want all the comapnies even if it is not associated with any attribute? Please explain
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)

Comments

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).")
";

Comments

0
"SELECT c.name from company c inner join  company_to_attributes cta ON cta.id_company = c.id_company WHERE   cta.id_attributes IN (1,2);"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.