i have two tables 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
note:class which is present in hcs wont be there in 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=$_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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.