Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The query including the joining of three table is not working?

Tags:

mysql

In the given code I am not able to join tbl_schedule with the other two table time(A),time(B):

$query="SELECT A.train_no AS AA, A.station_id AS AB, A.arrival AS AC, A.dept AS AD, B.station_id AS AE, B.arrival AS AF, B.dept AS AG FROM TIME AS A,TIME AS B,tbl_schedule WHERE A.train_no = B.train_no
AND A.station_id ='$f' AND B.station_id ='$t' AND sc_tr_num=A.train_no";
$rs=mysql_query($query);

while($row = mysql_fetch_array($rs))
{
        echo "<tr><td>".$row['AA']."</td> <td>".$stname1."</td> <td>" .$row['AC'] ."</td>
        <td>".$row['AD'] . "</td><td>".$stname2. "</td><td>".$row['AF'] . "</td><td>" .$row['AG']. "
        </td><td>".$row['sc_mon']."</td><td>".$row['sc_tue']."</td><td>".$row['sc_wed']."</td>
        <td>".$row['sc_thu']."</td><td>".$row['sc_fri']."</td><td>".$row['sc_sat']."</td>
        <td>".$row['sc_sun']."</td></tr>"."<a href='Reservation.php'>Click Me</a><tr><td>";
}
like image 305
Amit Avatar asked Nov 10 '22 14:11

Amit


1 Answers

Joining table can be done the following way (to be adapted to your needs)

Table A

+------+------
| idA  | valueA...

Table B

+------+------
| idB  | fkA (idA)...

Table C

+------+------
| idC  | fkC (idB)...

If you want to query over these 3 tables your query will look something like this:

SELECT A.valueA, B.valueB, C.valueC FROM A, B, C WHERE A.idA = B.fkA AND B.idB = C.fkC
like image 186
Stephane Paquet Avatar answered Nov 14 '22 23:11

Stephane Paquet