0

I'm using 2 functions. The first function pulls the following information:

$availcourse = "SELECT * FROM organization_courses oc left join course c on oc.courseid=c.id WHERE organizationid=$orgid ORDER By category";
$aresults = mysql_query($availcourse);

The second function pulls this information:

$depcourse = "SELECT * FROM organization_dep_courses odc left join course c on odc.courseid=c.id WHERE depid=$departmentid ORDER By category";
$dcresults = mysql_query($depcourse);

My question is, how would I keep records from function #1 from displaying in function #2 if they are the same? Obvious similarities in both results will be the courseid

I hope that makes sense.

4

1 回答 1

2

If organization_courses and organization_dep_courses have the same number of columns and the columns are in the same order, you can use MySQL's UNION.

$query = '
  SELECT *
  FROM (
    (SELECT *
     FROM organization_courses oc
     LEFT JOIN course c ON oc.courseid = c.id
     WHERE organizationid = $orgid)
    UNION
    (SELECT *
     FROM organization_dep_courses odc
     LEFT JOIN course c ON odc.courseid = c.id
     WHERE depid = $departmentid)
  ) q
  GROUP BY q.courseid
  ORDER BY q.category
';

Note: Your SQL code is vulnerable to SQL Injection.

于 2013-03-31T03:23:15.587 回答