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

  • Home
  • SEARCH
  • 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 3607376
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:22:05+00:00 2026-05-18T21:22:05+00:00

I have the following database structure: CREATE TABLE LookupTable ( PK UNIQUEIDENTIFIER PRIMARY KEY,

  • 0

I have the following database structure:

CREATE TABLE LookupTable
(
    PK UNIQUEIDENTIFIER PRIMARY KEY,
)

CREATE TABLE MainTable
(
    Lookup1 UNIQUEIDENTIFIER FOREIGN KEY REFERENCES LookupTable(PK),
    Lookup2 UNIQUEIDENTIFIER FOREIGN KEY REFERENCES LookupTable(PK),
    -- ...
    -- ... LookupN UNIQUEIDENTIFIER FOREIGN KEY REFERENCES LookupTable(PK),
)

MainTable references LookupTable multiple times via separate columns.

If I insert the following data:

INSERT INTO LookupTable VALUES('11111111-1111-1111-1111-111111111111')
INSERT INTO LookupTable VALUES('22222222-2222-2222-2222-222222222222')
INSERT INTO MainTable VALUES('11111111-1111-1111-1111-111111111111','22222222-2222-2222-2222-222222222222')
INSERT INTO MainTable VALUES('22222222-2222-2222-2222-222222222222','11111111-1111-1111-1111-111111111111')

I want to be able to find every record in [MainTable] where ANY of the lookup fields is equal to ‘11111111-1111-1111-1111-111111111111’ (this should return both rows in the example).

SQL is not my strong suit. Is there a simpler way of doing this than

SELECT * FROM MainTable WHERE
Lookup1 = '11111111-1111-1111-1111-111111111111'
OR
Lookup2 = '11111111-1111-1111-1111-111111111111'
-- ...
-- OR
-- LookupN = '11111111-1111-1111-1111-111111111111'

?

This seems tedious because it requires me to specify every lookup column by name before I can retrieve the results I want, and in my database there can be 20+ lookup columns in some circumstances.

  • 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-18T21:22:06+00:00Added an answer on May 18, 2026 at 9:22 pm

    There are three options:

    • Query your tables the way you doing (many ORs)
    • Build your query dynamically and execute it (like EXEC on SQL Server)
    • Change your database schema and move the Lookup-columns from your MainTable to a third table

    CREATE TABLE LookupTable
    (
        PK UNIQUEIDENTIFIER PRIMARY KEY,
    )
    
    
    CREATE TABLE MainTable
    (
        PK UNIQUEIDENTIFIER PRIMARY KEY,
    )
    
    CREATE TABLE MainTableLookup
    (
        MainTablePK UNIQUEIDENTIFIER FOREIGN KEY REFERENCES MainTable(PK),
        Lookup UNIQUEIDENTIFIER FOREIGN KEY REFERENCES LookupTable(PK),
    )
    

    Then you can query like this:

    SELECT
        * 
    FROM 
        MainTable MT JOIN MainTableLookup ON MT.PK = MTL.MainTablePK
    WHERE
        EXISTS (SELECT 1 FROM LookupTable LT 
                WHERE LT.PK = MTL.Lookup
                      AND MTL.Lookup = '11111111-1111-1111-1111-111111111111')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database with the following structure: CREATE TABLE entity ( id SERIAL,
I have the following database table created thus: CREATE TABLE AUCTIONS ( ARTICLE_NO VARCHAR(20),
I have the following table structure: CREATE TABLE [Report].[MesReport]( [MesReportID] [int] IDENTITY(1,1) NOT NULL,
I have the following table relationship in my database: Parent / \ Child1 Child2
So I have the following user defined type in my oracle database: CREATE OR
i have got a problem with my SQL Statement: Table structure: CREATE TABLE tags
I'm using EF v1. I have following tables: CREATE TABLE category ( category_id int
I have the following mysql table schema: SET SQL_MODE=NO_AUTO_VALUE_ON_ZERO; -- -- Database: `network` --
I have the following tables in my database that have a many-to-many relationship, which
I have the following function that is pulling data from a database. The ajax

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.