0

I am trying to write a search function for a page and there are many tables related to my franchise table and connected through a franchise table id. So a name of a city can be in the city table but the city is tied to a location table which is tied to the franchise table.

We have an inner join written and it does select the correct rows, but if the term is found in multiple tables or in multiple places in a table, it returns a row for each time that it is found. Is there a way to limit that. I am thinking a group would do it, but not sure where I would say "GROUP BY franchise.franchise_id"

            SELECT * FROM franchises
            INNER JOIN
            locations
            ON
            locations.franchise_id = franchises.franchise_id
            INNER JOIN
            operators
            ON
            operators.operator_id = franchises.operator_id
            INNER JOIN
            state_owners
            ON
            state_owners.state_owner_id = franchises.state_owner_id
            WHERE
            franchises.franchise_name             LIKE :term OR
            franchises.franchise_status           LIKE :term OR
            locations.location_name               LIKE :term OR
            locations.location_state              LIKE :term OR
            operators.operator_first_name         LIKE :term OR
            operators.operator_last_name          LIKE :term OR
            operators.operator_id                 LIKE :term OR
            state_owners.state_owner_first_name   LIKE :term OR
            state_owners.state_owner_last_name    LIKE :term OR
            state_owners.state_owner_owned_state  LIKE :term
Tyler Lazenby
  • 409
  • 5
  • 27
  • 1
    Please provide a relevant and minimal sample data showcasing your requirements, and expected output. Refer this link on how to frame a good SQL question: [Why should I provide an MCVE for what seems to me to be a very simple SQL query?](https://meta.stackoverflow.com/q/333952/2469308) – Madhur Bhaiya Nov 12 '18 at 21:11
  • 1
    Do read: [Why is SELECT * considered harmful?](https://stackoverflow.com/q/3639861/2469308) – Madhur Bhaiya Nov 12 '18 at 21:13
  • 1
    Instead of `SELECT *`, you can restrict the number of columns you really need, and then use `SELECT DISTINCT ...` – Madhur Bhaiya Nov 12 '18 at 21:13
  • Hi. This is a faq. Please always google many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on [mcve]. – philipxy Nov 12 '18 at 21:24

0 Answers0