开发者

SQL Query from 3 tables

开发者 https://www.devze.com 2023-04-05 09:09 出处:网络
Using PHP and mySQL. I have 4 total tables, AgencyInfo, TypeCodes, PrimarySta and MutualAid.AgencyInfo has unique ID as the primary.Typecodes has unique TID as the primary.PrimarySta and MutualAid hav

Using PHP and mySQL. I have 4 total tables, AgencyInfo, TypeCodes, PrimarySta and MutualAid. AgencyInfo has unique ID as the primary. Typecodes has unique TID as the primary. PrimarySta and MutualAid have both ID and TID together as their primary key.

I'm trying to create a recordset that will give me records from PrimarySta and MutualAid based on a ID from agencyinfo. There are 47 static records in TypeCodes. PrimarySta and MutualAid will have 47 entries(typecodes) per ID. PrimarySta has ID, TID and 12 INT fields, MutualAid has the same ID, TID and 12 INT fields plus a 13th field(text) called notes.

I'm using a while loop to step through the data. ID is passed via POST or SESSION.

Page Layout
                               Primary Station
TypeCode              P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12
                               Mutual Aid
                      M1 M2 M3 M4 M5 M6 M7 M8 M9 M10 M11 M12
Notes(M13)

This is repeated 47 times, once for each typecode. I used Dreamweaver to assemble my initial SQL Query, but from my research, it appears it may not be using the best syntax. I had the following code working with only 2 tables, TypeCodes and PrimarySta, 开发者_StackOverflow中文版but when I added in MutualAid, something broke.

$colname_Display = $_SESSION['sta_id']; }
mysql_select_db($database_DB, $MyDB);
$query_Display = sprintf("SELECT * FROM primarysta, typecodes, mutualaid 
WHERE primarysta.tid, mutualaid.tid = typecodes.tid AND primarysta.id = %s", 
GetSQLValueString($colname_Display, "int"));

I'm guessing my problem is with the primarysta.tid, mutualaid.tid = typecodes.tid line. I've seen INNER JOIN used, but can't seem to get the syntax right.

Can anyone help me with proper syntax?

Thanks!


SELECT
    *
FROM
    primarysta a
INNER JOIN
    typecodes b
ON
    a.tid = b.tid
INNER JOIN
    mutualaid c
ON 
    a.tid = c.tid
    and c.tid = b.tid
WHERE 
    a.id = %s -- where the %s is your string
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号