简体   繁体   English

哪种使用查询的方式会有效?

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

i have two tables hsc,sslc hsc: 我有两个表hsc,sslc hsc:

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

sslc SSLC

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

note:class which is present in hcs wont be there in sslc. 注意:hcs中存在的类不会出现在sslc中。 i should pass only id and class as a condition to get the result using php but i wont pass the table name.so i am using using union query 我应该只传递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);

OR 要么

$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);

both query result the same output . 两个查询结果相同的输出。 but will is an effective way? 但是会是一种有效的方式吗?

First one is effective way 第一个是有效的方法
A single call will always be faster than several calls for the same data.Simply the network turn around and latency alone is a component, but also the start up and tear down of the SQL processing will have some impact. 单个调用总是比对同一数据的几次调用更快。只是网络转向并且延迟本身就是一个组件,但SQL处理的启动和拆除也会产生一些影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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