1

I've searched a bit but haven't found exactly what I'm looking for, so far. Basically I have a MySQL database with a couple tables (keywords, company and link tables). I want to be able to supply an array of keywords and find the associated companies. When I run the second query without the WHERE IN clause, it works. When I supply an array, it doesn't.

select  keywords.idkeywords into @keyId
from keywords where
keywords.keyword IN ('Entertainment', 'Public');

select distinct company.company_name
from keywords, keyword_to_company, company 
where keyword_to_company.keywordId = @keyId
  and keyword_to_company.compId = company.idcompany;

2 Answers 2

2

Your query just doesn't make sense. First, you are trying to put multiple values in @keyid, but you can't do that. And, MySQL doesn't have the concept of table variables. You could use a temporary table.

Then the second query is worse. Does this query work for you?

select distinct c.company_name
from keywords k natural join
     keyword_to_company k2c natural join
     company c
where k.keyword IN ('Entertainment', 'Public') and
      k2c.compId = company.idcompany;

I'm only using natural join because you don't specify the join keys. In general, you should always specify the columns being joined, using either on or using.

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

Comments

0

Thanks... Your query didn't work..

This query does work however. Althought @keyId returns multiple rows, the query succeeds and results in a listing of associated companies. i agree that it shouldn't work, but it does.

select keywords.idkeywords into @keyId from keywords where keywords.keyword = 'Entertainment' and keywords.keyword = 'Public';

select distinct company.company_name from keywords, keyword_to_company, company where

keyword_to_company.keywordId = @keyId

and keyword_to_company.compId = company.idcompany;

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.