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 3997154
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:22:02+00:00 2026-05-20T07:22:02+00:00

I have a strange query to perform from a website. I have sets of

  • 0

I have a strange query to perform from a website. I have sets of arrays that contain pertinent ids from a many tables – 1 table per array. For example (the array name is the name of the table):

Array Set 1:
array "q": 1,2,3
array "u": 1,5
array "k": 7

Array Set 2:
array "t":   2,12
array "o":  8, 25

Array Set 3 (not really a set):
array "e": 5

I have another table, Alignment, which is not represented by the arrays. It performs a one to many relationship, allowing records from tables q,u, and k (array set 1, and recorded as relType/relID in the table) to be linked to records from t and o (array set 2, recorded as keyType/keyID) and e (array set 3, recorded as keyType/keyID). Example below:

Table: Alignment
 id   keyType  keyID   relType  relID 
 1       e       5        q       1
 2       o       8        q       1
 3       o       8        u       1
 4       t       2        q       2
 5       t       2        k       7
 6       t      12        q       1

So, in record 6, a record with an id of 12 from table t is being linked to a record with an id of 1 from table q.

I have to find missing links. The ideal state is that each of the ids from array set 1 have a record in the alignment table linking them to at least 1 record from array set 2. In the example, alignment record 1 does not count towards this goal, because it aligns a set 1 id to a set 3 id (instead of set 2).

Scanning the table, you can quickly see that there are some missing ids from array set 1: “q”-3 and “u”-5.

I’ve been doing this with script, by looping through each set 1 array and looking for a corresponding record, which generates a whole bunch of sql calls and really kills any page that calls this function.

Is there some way I could accomplish this in a single sql statement?

What would I like the results to look like (ideally):

recordset (consisting magically of data that didn’t exist in the table):

relType   |    relID
    q            3
    u            5

However, I would be elated with even a binary type answer from the database – were all the proper ids found: true or false? (Though the missing records array is required for other functions, but at least I’d be able to choose between the fast and slow options).

Oh, MySQL 5.1.

User Damp gave me an excellent answer using a temporary table, a join, and an IS NULL statement. But it was before I added in the wrinkle that there was a third array set that needed to be excluded from the results, which also ruins the IS NULL part. I edited his sql statement to look like this:

SELECT *
FROM k2
LEFT JOIN alignment
USING ( relType, relID )
HAVING alignment.keyType IS NULL
OR alignment.keyType = "e"

I’ve also tried it with a Group By relID (i always thought that was a requirement of the HAVING clause). The problem is that my result set includes “q”-1, which is linked to all three types of records (“o”,”t”, and “e”). I need this result excluded, but I’m not sure how.


Here’s the sql I ended up with:

SELECT *
FROM k2
LEFT JOIN (

    SELECT *
    FROM alignment
    WHERE keyType != 'e' and 
    ( 
    (relType = 'q' AND relID IN ( 1, 2, 3 ))
    OR 
    (relType = 'u' AND relID IN ( 1, 5 ))
    OR 
    (relType = 'k' AND relID IN ( 7 ))
    )

    )A
    USING ( relType, relID )
    HAVING keyType Is Null

I have to dump the values for the IN qualifiers with script. The key was not to join to the alignment table directly.

  • 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-20T07:22:02+00:00Added an answer on May 20, 2026 at 7:22 am

    You can try to go this route:

    DROP TABLE IF EXISTS k2;
    CREATE TEMPORARY TABLE k2 (relType varchar(10),relId int);
    INSERT INTO k2 VALUES 
      ('q',1),
      ('q',2),
      ('q',3),
      ('u',1),
      ('u',5),
      ('k',7);
    SELECT * FROM k2
    LEFT JOIN Alignment USING(relType,relId)
    HAVING Alignment.keyType IS NULL 
    

    This should work well for small tables. Not sure about very large ones though…

    EDIT

    If you wanted to add a WHERE statement the query would be as follow

    SELECT * FROM k2
    LEFT JOIN Alignment USING(relType,relId)
    WHERE Alignment.keyType != 'e'
    HAVING Alignment.keyType IS NULL 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I found a strange problem that is I have a Table With a
Strange performance outcome, I have a LINQ to SQL query which uses several let
I have a strange, sporadic issue. I have stored procedure that returns back 5
I have a strange problem with mod_rewrite, the rules that are relevant here are:
I have a strange bug where I call an insert query, it executes successfully
I have strange problem on my pc/server. I run query such as Select field1,
I have very strange problem with mySQL and simple query with simple index. I
I've mixed up with a strange behavior of MySQL query. I have next mysql
I have a bit of a strange problem with MySQL that I've never seen
I am getting some strange behavior involving database queries that I have never seen

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.