Compruebe si los valores de dos columnas de dos tablas son iguales y luego regrese

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'];
}

preguntado el 01 de julio de 12 a las 20:07

If you want to test row by row the results, whether you do it in PHP or MySQL, you better add ORDER BY something in your queries. Otherwise, all you got is inconsistent results. Nothing more. -

1 Respuestas

This is a strange requirement, since it debemos 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 debe: 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);

Actualizar:

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 es un DATE or DATETIME, Utilizar 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))

Respondido 01 Jul 12, 21:07

yeah,I need to apply 3 functions to the year field BEFORE I cross match it, substr is one of them I know that exists in mysql but the other 2 functions I wrote and I am not sure if its possible to do it all with a LEFT JOIN - cppit

@fogsy You can typically join against the result of function calls. If the functions you wrote are string operations or numeric operations, it is likely they can be duplicated in SQL. - Michael Berkowski

@fogsy: If you showed a few rows from the 2 tables and described how you want to match rows (or what the functions do), you could get an answer in SQL. - ypercubeᵀᴹ

@fogsy Doing it with days instead of years is more complicated because DAY() returns only the current day of the month. If they are always in the current year, you can use DAYOFYEAR() como en automail.days=(DAYOFYEAR(CURDATE()) - DAYOFYEAR(coverages.cvg_date)) - Michael Berkowski

@fogsy Don't use SUBSTRING() to strip off time. Use DATE() to return only the date portion of a DATETIME. - Michael Berkowski

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.