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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:22:23+00:00 2026-05-29T09:22:23+00:00

I wanted to see if I can better organize / optimize my code, and

  • 0

I wanted to see if I can better organize / optimize my code, and so I’ve been reading more about joins and how you can query / select from two different tables where a certain column matches up in a single query. However, I could not find any documentation on what I would like to do.

Consider two tables (A , B).


Table A
user_id -- + -- course_id
1       -- + -- 1

Table B
course_id -- + -- project_id
1         -- + --    2

My queries look something along the lines of the following:

$sql_course= mysql_query("SELECT course_id FROM A WHERE user_id = 1") or die(mysql_error());

 while ($course_row = mysql_fetch_assoc($sql_course)) {

                            // Unique course ID
                            $courseID = $course_row['course_id'];

                            $sql_b= mysql_query("SELECT project_id FROM B WHERE course_id=$courseID") or die(mysql_error());

So, you see, this is not very easy to explain. I suppose what I’m looking to find out is whether or not there is a way to optimize this code, say, using one query?

  • 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-29T09:22:24+00:00Added an answer on May 29, 2026 at 9:22 am

    Try:

    $sql_course = mysql_query("SELECT A.course_id, B.project_id FROM A INNER JOIN B ON B.course_id = A.course_id WHERE A.user_id = 1 ORDER BY A.course_id, B.project_id") or die(mysql_error());
    

    You only need to include A.course_id in the select portion if you care about the value.

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

Sidebar

Related Questions

I wanted to see if there is a cleaner and more effective way of
I'm brain tired and stumped so wanted to see if anyone knows a better
I wanted to see if folks were using decimal for financial applications instead of
I wanted to see what folks thought were the best way to help junior
I wanted to see what happened if you change the reference of an object
I wanted to see what some suggested approaches would be to validate a field
I just wanted to see if I could have your thoughts on the design
I just wanted to see if anyone is aware of any other debuggers capable
I see there is full ajax mode but I wanted to see if there
Im making an application in cocoa and wanted to see if some strings in

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.