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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:22:00+00:00 2026-06-09T09:22:00+00:00

I have two tables Courses & Students Say, Students has fields: id , first_name

  • 0

I have two tables Courses & Students

Say, Students has fields: id, first_name
with data

id : first_name

1 : Andrew
2 : Brian
3 : Charles
4 : David
5 : Eric
6 : Fred
7 : George

Say, Courses has two fields: id & students

With data:

id . . . . . . . . 1 . . . . . . 2

students . . . 1,2,5. . . .3,4,6,7

Say I want to find the names of students in course 1

SELECT students FROM courses c WHERE c.id = 1;

yields 1,2,5 as expected

Also

SELECT @students := students FROM courses c WHERE c.id =1;

yields 1,2,5 as expected

And

SELECT s.first_name FROM students s WHERE s.id IN (1,2,5);

yields Andrew, Brian Eric as expected

SET @students := students FROM courses c WHERE c.id =1;
SELECT @students;`

yields 1, 2, 5

I would expect

SET @students := students FROM courses c WHERE c.id =1;
SELECT s.first_name FROM students s WHERE s.id IN (@students);

to yield Andrew, Brian Eric

but it only returns the first name: Andrew

I’m fully aware that I could/should have a separate course_student look-up table but I’m keen to understand why I’m not getting the result I’m expecting

Where am I going wrong?

  • 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-06-09T09:22:03+00:00Added an answer on June 9, 2026 at 9:22 am

    Your problem is in how you’re using @students; it’s defaulting to string* when used in the ‘IN’ clause.

    So your query actually looks like:

    SELECT s.first_name FROM students s WHERE s.id IN ("1, 2, 5");
    

    Unfortunately, this ends up as:

    SELECT s.first_name FROM students s WHERE s.id IN (1);
    

    Either:

    SELECT s.first_name 
    FROM students s 
    WHERE s.id IN (
                   SELECT students FROM courses c WHERE c.id =1
                   )
    

    (A classic correlated subquery)

    Or:

    SELECT s.first_name FROM students s WHERE FIND_IN_SET(`id`, @students)
    

    Or, more preferably, use an INNER JOIN as @JohnTotetWoo describes.

    *It appears to be defaulting to a string then the resultant cast to int strips off every number after the first comma. However, it could also be just a quirk of the IN clause that ignores everything else in the variable after the first number.

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

Sidebar

Related Questions

I have next input data, There are two tables create table Courses( id number(19)
I have two tables : teachers (teacher_id,teacher_name) courses (teacher_id,course_id) And I need to display
I have two tables, lets say table1 and table2 with common columns, id and
I have two tables: users and user_depts. Let's say (for this question) that users
I have two tables of concern here: users and race_weeks. User has many race_weeks,
I have two tables - 'business' and 'business_contacts'. The business_contact table has a many-to-one
I have two tables, 'user' and 'account' with a 1:1 relationship. account has a
I have two tables in a MySQL database, courses and sessions. I'm trying to
I have two mysql tables, courses and student_courses, I need to get unique course_names
I have two tables The First one contains information about staff who take courses

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.