PHP & MySQL: Query from 2 tables -
i have following tables:
jobs:
+--------+------------+------------------+ | job_id | company_id | job_title | +--------+------------+------------------+ | 123456 | 342186 | mysql dev needed | | 549201 | 175123 | php dev needed | | 784930 | 823491 | ui designer pls | +--------+------------+------------------+
companies:
+------------+--------------+---------------------+ | company_id | company_name | company_email | +------------+--------------+---------------------+ | 342186 | microsoft | microsoft@email.com | | 823491 | quora | quora@email.com | | 784930 | facebook | facebook@email.com | +------------+--------------+---------------------+
this current query getting jobs jobs table:
// jobs jobs table $result = mysql_query("select * jobs") or die(mysql_error()); // check empty result if (mysql_num_rows($result) > 0) { // looping through results // jobs node $response["jobs"] = array(); while ($row = mysql_fetch_array($result)) { // temp user array $job = array(); $job["job_id"] = $row["job_id"]; $job["company_id"] = $row["company_id"]; $job["company_name"] = $row["company_name"]; //<<<-------this $job["job_title"] = $row["job_title"]; // push single product final response array array_push($response["jobs"], $product); } else { //error }
with above code jobs, now, how can modify query/code "company_name" belongs respective job? example:
since job "mysql dev needed" posted company_id = 342186 belongs microsoft
i thinking in making while loop inside actual while loop can exact company info, not believe best way.
select * jobs left join companies on jobs.company_id = companies.company_id
this 'join' 2 queries , give information in 1 row, 'joined' common identifier of both tables (company_id)
Comments
Post a Comment