Wednesday, June 22, 2011

Select from multi tables in MySQL



I have two tables cesei_members and cesei_committee_status which both use member_id as
primary key. I want to select elements from cesei_members based on the condition on cesei_committee_status, below is MySQL command:
SELECT A.member_id, A.first_name, A.last_name, A.phone, A.email, A.city, A.country, A.prefix FROM cesei_members A, cesei_committee_status B where A.member_id=B.member_id AND B.$comm_status>4;

Below is the PHP function using this MySQL query:
function get_member_list1()
{
global $db;
$member_list = array();

$comm_status='c1_status';

$sql = "SELECT A.member_id, A.first_name, A.last_name, A.phone, A.email, A.city, A.country, A.prefix FROM cesei_members A, cesei_committee_status B where A.member_id=B.member_id AND B.$comm_status>4";

$result = mysql_query($sql, $db);
while ($row = mysql_fetch_assoc($result)){
$member_list[$row['member_id']] = $row;
}
return $member_list;
}

No comments:

Post a Comment