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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:01:33+00:00 2026-06-08T06:01:33+00:00

I am trying to clean up my SQL queries and use JOIN in just

  • 0

I am trying to clean up my SQL queries and use JOIN in just ONE where I once used TWO queries.

Here is former code (in PHP):

$cat = "books";  // as a test 

$query = "SELECT category, cat_id FROM master_cat WHERE category = '{$cat}'";
$result = mysql_query($query);

while ($row = mysql_fetch_array($result)) {
    $cat_id = $row['cat_id'];
}

$sql = mysql_query("SELECT item, spam, cat_id FROM items WHERE cat_id = ' " . $cat_id . "' ORDER BY TRIM(LEADING 'The ' FROM item) ASC;");  

while ($row = mysql_fetch_assoc($sql))
    if ($row['spam'] < 2)
        $output[] = $row;


print(json_encode($output));  // added!

I am trying to just remove the top query and use a JOIN. The updated SQL statement is this:

EDIT: I made a mistake in original question. Basically user input gives us $cat = “something”. There is “something” in master_cat table with a cat_id. That cat_id is also in the items table. That is where I need the tables to connect — and the WHERE clause needs to incorporate “$cat”

UPDATED QUERY:

    $result = mysql_query("SELECT i.item, i.spam, mc.cat_id AS Category
FROM items as i
INNER JOIN master_cat as mc
ON i.cat_id = mc.cat_id
WHERE i.cat_id = '{$cat}'
ORDER BY TRIM(LEADING 'The ' FROM i.item) ASC;");

then:

   while ($row = mysql_fetch_assoc($result))
    if ($row['spam'] < 2)
        $output[] = $row;

I receive this in the browser:
null.

Can someone guide me on how to properly use JOIN which I know will REALLY clean things up here and make more efficient coding. I just watched a tutorials but still am not quite getting it.

HERE IS FINAL CODE THAT WORKS

$cat = $_POST['category']; // yes, yes, injection.  this is just the short version.

$result = mysql_query("SELECT i.item, i.cat_id, i.spam,  mc.cat_id, mc.category, TRIM(LEADING 'The ' FROM i.item) as CleanItem
FROM items as i
INNER JOIN master_cat as mc
ON i.cat_id = mc.cat_id
WHERE mc.category = '{$cat}'
ORDER BY CleanItem ASC;");

while ($row = mysql_fetch_assoc($result))
    if ($row['spam'] < 2)
        $output[] = $row;
  • 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-08T06:01:34+00:00Added an answer on June 8, 2026 at 6:01 am

    You have reference the items table by its full name in the WHERE clause, you should be using the alias you created (i).

    You also have an ambiguous column reference item in your ORDER BY clause.

    Try changing the last two lines to:

    WHERE i.cat_id = '{$cat_id}'
    ORDER BY TRIM(LEADING 'The ' FROM i.item) ASC
    

    You should also inspect mysql_error() to get a string description of the error, which would have pointed you straight to the problem.

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

Sidebar

Related Questions

I'm trying to use jquery with a simple php code: $('#some').click(function() { <?php require_once('some1.php?name=some'
I'm trying to clean duplicate code. The only difference are calls like MyType x
I'm trying to clean up some warnings in some old Java code (in Eclipse),
I'm running SQL Server 2008, and trying to run some queries on some poorly
I'm trying to write an UPDATE SQL Query to clean up my database a
I'm trying to wrap some repetitive code in an extension method, to clean things
I'm trying to clean a recently imported sql server 2008 database that have to
I'm using EF CTP5 code-first with Sql CE 4.0. I am trying to perform
I'm trying to simulate some code that I have working with SQL but using
I'm trying to setup my project so that I can use querydsl-sql against a

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.