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

The Archive Base Latest Questions

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

I have 2 tables in my database. One is for orders, and one is

  • 0

I have 2 tables in my database. One is for orders, and one is for companies.

Orders has this structure:

OrderID     |     attachedCompanyIDs
------------------------------------
   1                     1,2,3
   2                     2,4

And Company has this structure:

CompanyID      |        name
--------------------------------------
    1                 Company 1
    2                 Another Company
    3                 StackOverflow
    4                 Nothing

To get an order’s companies names, I can do a query as such:

SELECT name FROM orders,company
WHERE orderID = 1 AND FIND_IN_SET(companyID, attachedCompanyIDs)

That query works fine, but the following query does not.

SELECT name FROM orders,company
WHERE orderID = 1 AND companyID IN (attachedCompanyIDs)

Why does the first query work but not the second one?

The first query returns:

name
---------------
Company 1
Another Company
StackOverflow

The second query only returns:

name
---------------
Company 1

Why is this, why does the first query return all the companies, but the second query only returns the first one?

  • 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-18T00:22:33+00:00Added an answer on May 18, 2026 at 12:22 am
    SELECT  name
    FROM    orders,company
    WHERE   orderID = 1
            AND companyID IN (attachedCompanyIDs)
    

    attachedCompanyIDs is a scalar value which is cast into INT (type of companyID).

    The cast only returns numbers up to the first non-digit (a comma in your case).

    Thus,

    companyID IN ('1,2,3') ≡ companyID IN (CAST('1,2,3' AS INT)) ≡ companyID IN (1)
    

    In PostgreSQL, you could cast the string into array (or store it as an array in the first place):

    SELECT  name
    FROM    orders
    JOIN    company
    ON      companyID = ANY (('{' | attachedCompanyIDs | '}')::INT[])
    WHERE   orderID = 1
    

    and this would even use an index on companyID.

    Unfortunately, this does not work in MySQL since the latter does not support arrays.

    You may find this article interesting (see #2):

    • 10 things in MySQL (that won’t work as expected)

    Update:

    If there is some reasonable limit on the number of values in the comma separated lists (say, no more than 5), so you can try to use this query:

    SELECT  name
    FROM    orders
    CROSS JOIN
            (
            SELECT  1 AS pos
            UNION ALL
            SELECT  2 AS pos
            UNION ALL
            SELECT  3 AS pos
            UNION ALL
            SELECT  4 AS pos
            UNION ALL
            SELECT  5 AS pos
            ) q
    JOIN    company
    ON      companyID = CAST(NULLIF(SUBSTRING_INDEX(attachedCompanyIDs, ',', -pos), SUBSTRING_INDEX(attachedCompanyIDs, ',', 1 - pos)) AS UNSIGNED)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a poorly designed database. One of the most important tables has 11,000+
I have a couple of tables in my database, one called Task , and
I have a database with 2 tables. One of the tables holds a row
In my database, I have a few columns in one of my tables that
I have a SQL Mobile database with one table. It has several columns with
I have noticed a lot of companies use a prefix for their database tables.
I have a database table and one of the fields (not the primary key)
Many applications have grids that display data from a database table one page at
I have a database table with a large number of rows and one numeric
I have a database with one table, like so: UserID (int), MovieID (int), Rating

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.