简体   繁体   中英

I cannot think of a valid SQL Query for solving this

I have the following SQL Tables:

companies:

╔═════╦══════════╦═════════╦═══════════╦══════════════════╦═════╗
║  #  ║   Name   ║ Country ║   City    ║     Address      ║ Nr. ║
╠═════╬══════════╬═════════╬═══════════╬══════════════════╬═════╣
║ 1   ║ T-Online ║ Germany ║ Frankfurt ║ Teststr.         ║  15 ║
║ 2   ║ Telecom  ║ Italy   ║ Rome      ║ Via test         ║  20 ║
║ 3   ║ Verizon  ║ USA     ║ New York  ║ Something street ║  53 ║
║ ... ║ ....     ║         ║           ║                  ║     ║
╚═════╩══════════╩═════════╩═══════════╩══════════════════╩═════╝

tagsForCompany:

╔═════════╦═════╗
║ Company ║ TID ║
╠═════════╬═════╣
║ 1       ║ 10  ║
║ 2       ║ 15  ║
║ 1       ║ 11  ║
║ 3       ║ 19  ║
║ 2       ║ 11  ║
╚═════════╩═════╝

tags:

╔════╦══════════════════════╗
║ ID ║ Name                 ║
╠════╬══════════════════════╣
║ 1  ║ Software Development ║
║ 2  ║ Hosting              ║
║ 3  ║ Restaurant           ║
║... ║ ....                 ║
╚════╩══════════════════════╝

(all the values are examples and are not what's on the real tables!)

I need to search for a company in a given city and country and have a specific tag. For example I search all the companies in New York, USA that have the tag Software Development. I need to be able to do this with only one SQL Query.

What I'm using right now:

  • I search for all the companies in the given city and country
  • Then search for the id of the given tag
  • After that I search all the companies that have that Tag ID
  • And at the end I filter the table companies to output all the companies that match those rules.

Obviously this method isn't good to be used in production, the impact to the performance is too big to be used.

EDIT: Thank you for all the answers, I will try each one of them and the one that works best will be the approved one :)

Well you can use JOIN and add index on country and city in companies table

SELECT Name
FROM companies AS c INNER JOIN tagsForCompany AS tc ON c.id = tc.Company
INNER JOIN tags AS t ON t.id = tc.TID
WHERE city = "your_city" AND country = "your_country" AND t.Name REGEXP 'your_tag'

Well in this query a table will be generated first using INNER JOIN then filtering on basis of new table generated

But a better and more optimized solution could be to generate a table using subquery by filtering city and country. Also add index on country and city in companies table as it will reduce a lot of time. Now new query would be

SELECT c.Name
FROM 
  (SELECT id, Name
   FROM companies
   WHERE city = "your_city" AND country = "your_country" ) AS c
  INNER JOIN tagsForCompany AS tc ON c.id = tc.Company
  INNER JOIN tags AS t ON t.id = tc.TID
WHERE t.Name REGEXP 'your_tag'

Syntax to add index ALTER TABLE tbl_name ADD INDEX index_name (column_list) , it adds an ordinary index in which any value may appear more than once.

ALTER TABLE companies ADD INDEX city (city);
ALTER TABLE companies ADD INDEX country (country);

try this

SELECT *
FROM companies c, tags t, tagsForCompany tfc
WHERE tfc.company = c.companyId
AND   t.ID = tfc.TID
AND   t.Name = 'Software Development'
AND   c.city = 'New York'
AND   c.Country = 'USA'

Make sure to connect the proper rows together before searching.

select *
from companies c
inner join tagsForCompany tc on tc.Company = c.#
inner join tags t on t.ID = tc.TID
where c.Country = '<param>' and c.City = '<param>' and t.Name = 'Software Development'

try below:

select *
from companies
where companyid in (
    select Company
    from tagsForCompany TID = 1 and Company in (
        Select companyid
        from companies where city ='New York' and country ='USA'
    ) a
) b

May this query is help you.

select * from companies c
left join tagsForCompany tc on c.company_id = tc.company_id
left join tags t on t.tag_id = tc.tag_id
where c.city = "your value" and c.country = "Your value" and tc.name = "your value"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM