Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7458983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:55:31+00:00 2026-05-29T13:55:31+00:00

Consider the SQL query below: SELECT DISTINCT shops.*, DA.delivery_cost, DA.postcode FROM shops JOIN shops_delivery_area

  • 0

Consider the SQL query below:

SELECT DISTINCT shops.*, 
       DA.delivery_cost, 
       DA.postcode 
FROM shops 
       JOIN shops_delivery_area as DA on DA.shop_id = shops.id 
WHERE DA.postcode = "Liverpool" 
      OR location = "Liverpool"

shops table

+----+----------+-----------+----------+
| id | name     | location  | postcode |
+----+----------+-----------+----------+
|  1 | Shop One | Liverpool | L10      |
|  2 | Shop Two | Liverpool | L16      |
+----+----------+-----------+----------+

shops_delivery_area table

+------------------+---------+----------+---------------+
| delivery_area_id | shop_id | postcode | delivery_cost |
+------------------+---------+----------+---------------+
|                1 |       1 | L10      |          0.00 |
|                2 |       1 | L11      |          0.00 |
|                3 |       1 | L12      |          1.00 |
|                4 |       1 | L13      |          1.00 |
|                5 |       2 | L10      |          0.00 |
|                6 |       2 | L16      |          0.00 |
|                7 |       2 | L28      |          0.00 |
+------------------+---------+----------+---------------+

User can search by Postcode (eg: L14, L15, L16) or Location from a textbox.

If user type in “Liverpool”, it will find all the shops that are located in “Liverpool”. The problem is there will be duplicate rows of shop name (shops table). How to solve this issue?

Result (Search by Location):

How to avoid duplication of shop name?

I should do: shops.id = shops_delivery_area.shop_id AND shops.postcode = shops_delivery_area.postcode

+----+----------+-----------+----------+---------------+----------+
| id | name     | location  | postcode | delivery_cost | postcode |
+----+----------+-----------+----------+---------------+----------+
|  1 | Shop One | Liverpool | L10      |          0.00 | L10      |
|  1 | Shop One | Liverpool | L10      |          0.00 | L11      |
|  1 | Shop One | Liverpool | L10      |          1.00 | L12      |
|  1 | Shop One | Liverpool | L10      |          1.00 | L13      |
|  2 | Shop Two | Liverpool | L16      |          0.00 | L10      |
|  2 | Shop Two | Liverpool | L16      |          0.00 | L16      |
|  2 | Shop Two | Liverpool | L16      |          0.00 | L28      |
+----+----------+-----------+----------+---------------+----------+

Result (Search by Postcode L10):
Work fine as expected

+----+----------+-----------+----------+---------------+----------+
| id | name     | location  | postcode | delivery_cost | postcode |
+----+----------+-----------+----------+---------------+----------+
|  1 | Shop One | Liverpool | L10      |          0.00 | L10      |
|  2 | Shop Two | Liverpool | L16      |          0.00 | L10      |
+----+----------+-----------+----------+---------------+----------+
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T13:55:31+00:00Added an answer on May 29, 2026 at 1:55 pm

    I have managed to get it working. I have used UNION to solve this problem.

    SELECT U.* FROM 
       ((SELECT DISTINCT shops.*, DA.delivery_cost, DA.postcode AS AreaPostcode FROM shops
                 JOIN shops_delivery_area2 as DA on DA.shop_id = shops.id
       WHERE DA.postcode = "Liverpool")
      UNION
       (SELECT DISTINCT shops.*, DA.delivery_cost, DA.postcode AS AreaPostcode FROM shops
                 JOIN shops_delivery_area2 as DA on DA.shop_id = shops.id AND DA.postcode = shops.postcode
       WHERE shops.location = "Liverpool")) as U
    

    If there anyway to make this code shorter or improve it, let me know. thanks

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the SQL query below: SELECT * FROM opening_hours WHERE week_day = WEEKDAY(NOW()) +
Consider I have such SQL query: SELECT t1.* FROM table1 t1 LEFT JOIN table2
Consider an SQL query like this SELECT Temp.rating, Temp.avgage FROM (SELECT S.rating, AVG (S.age)
Consider the following SQL Server 2005/2008 query: Select [UID], [DESC] From SomeTable Order By
Consider the following 3 standard statements $queryString = SOME SQL SELECT QUERY; $queryResult =
How do i do a left join using FetchXml? Consider a simple SQL query
First off, here's my SQL query: SELECT research_cost, tech_name, (SELECT research_cost FROM technologies WHERE
Let's consider such query: SELECT * FROM ( SELECT YEAR(OrderDate) [Year], MONTH(OrderDate) [Month], SubTotal
Consider the following SQL: SELECT mov FROM movie WHERE mov IN ( SELECT mov
consider my sql query below; it is calling sum twice for the same argument.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.