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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:27:50+00:00 2026-06-06T22:27:50+00:00

I have 2 tables as follow : table1 has these fields: year,day,month,name table2 has

  • 0

I have 2 tables as follow :
table1 has these fields:

year,day,month,name

table2 has these fields:

years,gift

I want to return the rows where year = years. the two queries NEED to be run separately that is a condition.
I CANNOT join them into 1 query (I know how to do that but that’s not how this should be done). Any suggestions? I thought of using a foreach loop but failed to implement, any ideas?

$sql1 = " SELECT * FROM table1 ORDER BY id ASC";
  $result1 = mysql_query($sql1);
  $rows = mysql_fetch_array($result1);
    while($rows = mysql_fetch_array($result1))
  { 
  $year[]         = $rows['year'];
}

$sqlx = "SELECT * FROM table2";
  $result = mysql_query($sqlx); 

  while($row = mysql_fetch_array($result))
    {
$years[]         = $rows['years'];
}
  • 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-06T22:27:52+00:00Added an answer on June 6, 2026 at 10:27 pm

    This is a strange requirement, since it should be done with a join. It is vastly less efficient to do this with multiple queries in a loop than a single query with a JOIN.

    In any case if you must do it this way, you have retrieved the years into an array correctly from your first query — use that array to populate the query for each loop iteration of your second query:

      $year = array();
      $sql1 = " SELECT * FROM table1";
      $result1 = mysql_query($sql1);
      if ($result1) {
        $rows = mysql_fetch_array($result1);
        while($rows = mysql_fetch_array($result1))
        { 
          $year[] = $rows['year'];
        }
      }
    
      // Will hold your output
      $gifts = array();
      // $year is now an array of years.
      // Loop over it to query table2:
      foreach ($year as $y) {
        // Make sure it is an integer...
        $y = intval($y);
        // And query table2 using $y as a WHERE condition
        $sql2 = "SELECT years, gift FROM table2 WHERE years = $y";
        $result2 = mysql_query($sql2);
        if ($result2) {
           while($row = mysql_fetch_array($result2)) {
             // Append all rows to the $gifts array
             $gifts[] = $row;
           }
        }
      }
    
      // They're now all in the array $gifts.
      var_dump($gifts);
    

    Update:

    years on table 1 is a period in years. year in table is a date of when the account was created. so I have to find the difference from today’s date to year on table2. then see if it matches any of the rows on table1. and return the results that match

    This can be accomplished with a creative JOIN using date functions. Assuming table2.years is a DATE or DATETIME, use YEAR() to return only the year portion from it, and compare it to YEAR(CURDATE()) for the current year. Join that against the number of years in table1.year.

    SELECT 
      table1.*, 
      table2.*
    FROM 
      table1
      JOIN table2 
        ON table1.year = (YEAR(CURDATE()) - YEAR(table2.years))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables: Lecturer and Department . The Lecturer table has these columns:
i have 2 tables, users and follows. table follows has a column named status.
In a follow-up to a previous question, let's say I have 3 tables, A,
I'm using PHP/MySql. If I have two tables (hypothetically) as follows: id name and
I have a table1 (records 3), and table2 (records 3). Where i have field
I have the follow two mysql tables: mysql> desc subscriptions; +------------+--------------+------+-----+---------+----------------+ | Field |
I have a database containing three tables: practices - 8 fields patients - 47
I have two tables: Table 1 has Episode and Code, with Episode as distinct.
I have two tables with the following columns: Table1 { ID NUMBER(15), ROLL_NUM VARCHAR2(9),
I have a HTML page with about 50 tables on it. Each table has

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.