[英]Display top 10 results in codeigniter and display 1st, 2nd and 3rd top values
[英]Get the values from 2nd and 3rd table for the matched values in 1st table
我是一個jQuery開發人員,我不知道如何從表中檢索數據。 我只是想學習它。
我有一個特殊情況需要處理。 考慮三個表
tb1
-----------------------------------------------
pid fname lname cid eid
12 jo mo 16345 2345
13 ko ro 16324 2435
tb2
-----------------------------------------------
cid cname
16345 amazed
16324 bored
tb3
------------------------------------------------
eid ename
2345 nolo
21345 johny
我想檢索匹配的pid值的數據。 說,
select * from tb1 where pid = 12
它返回我pid, fname, lname, cid, eid
cid
值是16345,而不是數字。 我想要將cid
值與tb2
匹配並獲得amazed
的cname列值,並且與tb2
的eid
相同ename : nolo
我希望這些數字與tb2和tb3匹配並獲取它們的字符串值。
我可以使用3個MySQL查詢來完成它
這就是我嘗試過的
$result = mysql_query("select * from tb1 where pid = 12");
$row = mysql_fetch_array($result,MYSQL_ASSOC);
$cname = mysql_query("select cname from tb2 where cid = '$row[cid]'");
$x = mysql_fetch_array($cname,MYSQL_ASSOC);
$cname = mysql_query("select ename from tb3 where eid = '$row[eid]'");
$y = mysql_fetch_array($cname,MYSQL_ASSOC);
但我覺得代碼並不理想。 我想必須有一種方法可以在單個查詢中檢索,但我不確定它必須是什么。 也許加入或子查詢。
在3個表上使用連接
SELECT tb1.*, tb2.cname, tb3.ename from tb1
JOIN tb2 ON tb1.cid = tb2.cid
JOIN tb3 ON tb1.eid = tb3.eid
WHERE tb1.pid = 12
希望這可以幫助
嘗試這個:
SELECT *.t1, t2.cname, t3.ename from tb1 as t1
JOIN tb2 as t2 ON t1.cid = t2.cid
JOIN tb3 as t3 ON t1.eid = t3.eid
WHERE t1.pid = 12
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.