从其他表没有的表中获取结果

时间:2016-04-21 14:20:46

标签: php mysql pdo compare fetch

我正在尝试比较两个数组:

  

第一个数组

void gradeLetter (STUREC records[], int count)
{

int j;
char letter;
int s;
int i;

printf("Enter letter grade you wish to view: ");
scanf("%s", letter);

for(j = 0; j < count; j++)
    {s = strrchr (records[count].grade, "letter");

  if (s != NULL)
  {

    printf("\n+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");
    printf("| Student Name              |   ID   | Test 1 | Test 2 | Proj 1 | Proj 2 | Proj 3 | Average | Grade |\n");
    printf("+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");

    for (i = 0; i < count; i++)
    {
        int count = printf( "| %s, %s", records[i].lname, records[i].fname );
if ( count < NAME_LEN )
    printf( "%*s", NAME_LEN - count, "" ); printf ("| %-7.6d| %4d   | %4d   | %4d   | %4d   | %4d   | %6.2f  |  %-2s   |\n", records[i].id, records[i].score1,
                 records[i].score2, records[i].score3, records[i].score4, records[i].score5,
                  records[i].ave, records[i].grade);
    }
    printf("+---------------------------+--------+--------+--------+--------+--------+--------+---------+-------+\n");

    }
    }

  return 0;
}
  

第二个数组

$query = $db->prepare("SELECT vraagnummer FROM insertquestion");
$query->execute();
$fetch_query = $query->fetchAll(PDO::FETCH_ASSOC);

我希望收到$query = $db->prepare("SELECT vraagnummer FROM vraag"); $query->execute(); $fetch_query_exists = $query->fetchAll(PDO::FETCH_ASSOC);

$fetch_query所有结果的所有结果

我尝试使用:

fetch_query_exists

但是没有显示结果,只是一个空数组。

那么如何获取$result=array_diff($fetch_query ,$fetch_query_exists );的结果,不包括fetch_query中存在的结果?

4 个答案:

答案 0 :(得分:2)

您可以使用NOT IN

在单个查询中执行此操作
SELECT vraagnummer FROM insertquestion
  WHERE vraagnummer NOT IN(SELECT vraagnummer FROM vraag)

答案 1 :(得分:1)

除了@Saty的答案之外,我相信以下内容也会在没有子选择的情况下发挥作用。

SELECT i.vraagnummber 
FROM insertquestion i 
LEFT JOIN vraag v
    ON v.vraagnummer = i.vraagnummer
WHERE v.vraagnummer IS NULL;

答案 2 :(得分:0)

也许,但不确定:

$query = $db->prepare("SELECT vraagnummer FROM insertquestion RIGHT JOIN vraag ON insertquestion.vraagnummer = vraag.vraagnummer");
$query->execute();
$fetch_query = $query->fetchAll(PDO::FETCH_ASSOC);

答案 3 :(得分:0)

如果您不想使用单个查询,请使用:

$result = array_diff_assoc($fetch_query , $fetch_query_exists );