簡體   English   中英

哪種使用查詢的方式會有效?

[英]which way of using query will be efficient?

我有兩個表hsc,sslc hsc:

id         name           class   section
1          karna            1        a
2          bavi             2        b
3          chidu            3        c

SSLC

id         name           class    section
1          ram             11        a2
2          sam             11        b1
3          guna            14        c2

注意:hcs中存在的類不會出現在sslc中。 我應該只傳遞id和類作為條件,以獲得使用PHP的結果,但我不會傳遞表名。所以我使用聯合查詢

$id=$_REQUEST['id'];
$class=$_REQUEST['class'];

$sql="select t1.* (select id,name,class,section from hsc union select  id,name,class,section from sslc)t1 where id=$id and class=$class;"//this runs successfully 
$StudentArray = $Cobj->union($sql);

要么

$sql="select * from hsc where id=$id and class=$class;"
$StudentArray = $Cobj->union($sql);
if(count($StudentArray)>0){
$table="hsc";}
else{
$table="sslc";}
$sql="select * from $table where id=$id and class=$class";
$StudentArray = $Cobj->union($sql);

兩個查詢結果相同的輸出。 但是會是一種有效的方式嗎?

第一個是有效的方法
單個調用總是比對同一數據的幾次調用更快。只是網絡轉向並且延遲本身就是一個組件,但SQL處理的啟動和拆除也會產生一些影響。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM