简体   繁体   中英

how to use SQL UNION in where clause having variable in query

I'm trying to retrieve data from two tables using UNION but not works in my case. I'm trying this

    $folq = $db->query("select id as pid,folder,name as catname,name,thumb,pathc from hollywood where id = ".$parentid, database::GET_ROW) UNION ("select id as pid,folder,name as catname,name,thumb,pathc from bollywood where id = ".$parentid, database::GET_ROW);

But not works for me. Please suggest me how to achive that.

You need one string query (not two , and in the second query you don't need alias)

 $folq = $db->query("
  select 
      id as pid
      ,folder
      ,name as catname
      ,name
      ,thumb
      ,pathc 
  from hollywood 
  where id =  ".$parentid . 
  " UNION 
    select 
      id 
      ,folder
      ,name
      ,name
      ,thumb
      ,pathc 
  from bollywood 
  where id = ". $parentid

  , database::GET_ROW);

我认为您的查询中存在语法错误,应该是这样的

 $folq = $db->query("select id as pid,folder,name as catname,name,thumb,pathc from hollywood where id = ".$parentid . " UNION select id as pid,folder,name as catname,name,thumb,pathc from bollywood where id = ".$parentid, database::GET_ROW);

Please use this

        $folq = $this->db->query("
            select id as pid,folder,name as catname,name,thumb,pathc from hollywood where id = $parentid 
        UNION  
            select id as pid,folder,name as catname,name,thumb,pathc from bollywood where id = ".$parentid);

This works :)

         $folq = $db->query("
        select id as pid,folder,name as         catname,name,thumb,pathc from category where id         = $parentid 
           UNION  
            select id as pid,folder,name as         catname,name,thumb,pathc from punjabi where id         = ".$parentid, database::GET_ROW);

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.

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