I am trying to merge some columns from one table into a view that contains all the data from another table. So far I have failed, here is what I have so far:
$create = "CREATE VIEW filter AS SELECT `".$db."`.`leads`.*, (`".$db."`.applicants.name AS lead_fname, `".$db."`.applicants.email AS lead_email, `".$db."`.applicants.appdate AS lead_added ) FROM `".$db."`.`leads`, `".$db."`.`applicants` WHERE ".$filterTerms." AND `lead_status` = '1' ORDER BY `lead_added`";
Any help would be appreciated
I think this is what i am trying to achieve:
View = table1 + table2
ID | email | name | appdate
1 data data data
2 data data data
3 data data data
4 data data data
5 data data data
Table 1
ID | email | name | appdate
1 data data data
2 data data data
Table 2
ID | s_email | s_name | s_appdate
1 data data data
2 data data data
3 data data data
Based on the updated info, you want a UNION:
select email, name, appdate from table1
UNION
select s_email as email, s_name as name, s_appdate as appdate from table2
You need a column in the leads table that joins it to the applicants table (a foreign key). For example, if you have an application_id column in the leads table that matches the applicant_id column in the applicants table:
CREATE VIEW filter AS
SELECT L.*, A.name as lead_fname, A.email as lead_email, A.appdate as lead_added
FROM leads L, applications A
WHERE L.application_id=A.applicant_id;
You create the view in the database (using phpMyAdmin or some tool like that), not dynamically in your PHP script.
Then your PHP script can query the view as if it were a table.
$sql = "SELECT * FROM filter WHERE " . $filterTerms . "AND lead_status = '1' ORDER BY lead_added";