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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:17:10+00:00 2026-05-30T10:17:10+00:00

I would like to add a value to each row that I get from

  • 0

I would like to add a value to each row that I get from my query depending on if a row exist in another table. Is there a smart way to achieve this?

This is the code I have:

$sth = mysql_query("SELECT tbl_subApp2Tag.*, tbl_tag.* FROM tbl_subApp2Tag LEFT JOIN tbl_tag ON tbl_subApp2Tag.tag_id = tbl_tag.id WHERE tbl_subApp2Tag.subApp_id = '".$sub."' ORDER BY tbl_tag.name ASC");
if(!$sth) echo "Error in query: ".mysql_error();
while($r = mysql_fetch_assoc($sth)) {
    $query = "SELECT * FROM tbl_userDevice2Tag WHERE tag_id='".$r['id']."' AND userDevice_id='".$user."'";
    $result = mysql_query($query) or die(mysql_error());
    if (mysql_num_rows($result)) {
        $r['relation'] = true;
        $rows[] = $r; //Add 'relation' => true to this row
    } else {
        $r['relation'] = false;
        $rows[] = $r; //Add 'relation' => false to this row
    }
}
print json_encode($rows);

Where the //Add … is, is where I would like to insert the extra value. Any suggestions of how I can do this?
I’m still a beginner in PHP so if there are anything else that I have missed please tell me.

EDIT: Second query was from the wrong table. This is the correct 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-30T10:17:11+00:00Added an answer on May 30, 2026 at 10:17 am

    Edited Edited below query to reflect new information because I don’t like leaving things half-done.

    $sth = mysql_query("
        SELECT
            tbl_subApp2Tag.*,
            tbl_tag.*,
            ISNULL(tbl_userDevice2Tag.userDevice_id) AS relation
    
        FROM tbl_subApp2Tag
        LEFT JOIN tbl_tag
            ON tbl_tag.id = tbl_subApp2Tag.tag_id
    
        LEFT JOIN tbl_userDevice2Tag
            ON tbl_userDevice2Tag.tag_id = tbl_tag.id
            AND tbl_userDevice2Tag.userDevice_id = '".$user."'
    
        WHERE tbl_subApp2Tag.subApp_id = '".$sub."'
        ORDER BY tbl_tag.name ASC
    ");
    

    Though the above feels like the LEFT JOIN on tbl_tag is the wrong way around, but it’s hard to tell as you are vague on your eventual aim. For example, if I was to assume the following

    1. Tags will always exist
    2. subApp2Tag will always exist
    3. You want to know if a record in tbl_userDevice2Tag matches the above

    Then I would do the following instead. The INNER JOIN means that it won’t worry about records in tbl_tag that are not on the requested subApp_id which in turn will limit the other joins.

    $sth = mysql_query("
        SELECT
            tbl_subApp2Tag.*,
            tbl_tag.*,
            ISNULL(tbl_userDevice2Tag.userDevice_id) AS relation
    
        FROM tbl_tag
    
        INNER JOIN tbl_subApp2Tag
            ON tbl_subApp2Tag.tag_id = tbl_tag.id
            AND tbl_subApp2Tag.subApp_id = '".$sub."'
    
        LEFT JOIN tbl_userDevice2Tag
            ON tbl_userDevice2Tag.tag_id = tbl_tag.id
            AND tbl_userDevice2Tag.userDevice_id = '".$user."'
    
        ORDER BY tbl_tag.name ASC
    ");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to add some icons to my treeview. Is there a way
How do I get the value clicked in each row (generated using AddRowTable plugin),
I have a bunch a values I would like to add together which are
I would like to do something like add a nice-to-Excel-functions Name property to the
I would like to add the following MIME type to a site run by
I would like to add a DataGridViewTextBoxCell cell to a DataGridViewCell control, but as
I would like to add AES encryption to a software product, but am concerned
I would like to add graphing to my User Controls in ASP.NET MVC. I
I would like to add a typing speed indicator just below the textarea we
I would like to add a BuildListener to my headless build process, which is

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.