简体   繁体   中英

MySql query using two tables

I'm trying to get this query to work, I just want to display record where Physio Reference (from table 1) equals SESS_MEMBER_ID (from a members table in MySQL).

I'm not sure exactly how to do this as I'm still grasping the concept of php. I'm not sure if you have to put some code at the end to tell it that the field is from the table members .

$sql="SELECT * FROM IA 
      WHERE IASubmitted= 'no' 
      AND PhysioReference = 'SESS_MEMBER_ID'";
$result=mysql_query($sql);

Members table

member_id, firstname, lastname, login, passwd,

IA Table

Reference, Forename, Surname, DOB, PhysioReference,

Thanks in advance

update

$sql="SELECT * FROM IA tb1,members tb2 where tb1.PhysioReference=tb2.member_id and tb2.member_id=SESS_MEMBER_ID";
$result=mysql_query($sql);

Use INNER JOIN like this:

$sql="SELECT * FROM IA 
INNER JOIN MEMBERS_TABLE ON IA.PhysioReference = MEMBERS_TABLE.PhysioReference
WHERE IA.IASubmitted= 'no' AND IA.PhysioReference = 'SESS_MEMBER_ID'";
$result=mysql_query($sql);

More information about INNER JOIN: http://www.mysqltutorial.org/mysql-inner-join.aspx

Try this one

$sql="SELECT * FROM IA tb1,MEMBERS_TABLE tb2 where tb1.PhysioReference=tb2.member_id and tb2.member_id=SESS_MEMBER_ID";
$result=mysql_query($sql);

Note: tb2 pk :member_id and tb1 FK : PhysioReference then only the above query applicable

This Inner joined worked with my session varibles

$sql="SELECT * 
      FROM IA tb1, members tb2 
      WHERE tb1.PhysioReference=tb2.member_id
      AND tb2.member_id='{$_SESSION["MEMBER_ID"]}';
$result=mysql_query($sql);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM