使用UNION JOIN从多个用户类型中选择项目

时间:2013-12-28 03:37:01

标签: php mysql join

我在这里有一个问题。我有3个表,即tbl_ads, tbl_ps, tbl_dealer

tbl_ads ( ad_id, ad_type, poster_id,  cat,  title, description, images ... , ad_status )

tbl_ps ( ps_id, name , address, city, state )

tbl_dealer ( id, name , address, city, state, pack, bussname )

现在我想根据title中的descriptiontbl_ads搜索广告,但我想根据citystate搜索广告它们位于不同的表中,具体取决于tbl_ads

基本上在这里,我希望获得广告,如果ad_type = 'ps'然后从tbl_psif ad_type = 'dealer'获取然后从tbl_dealer获取...表ad和{之间的关系{1}} ps ,表tbl_ps.ps_id = poster_idad之间的关系 dealer

问题是,我想根据广告tbl_dealer.id = poster_id,广告titledescriptioncity搜索广告,并获得仅有tbl_ads记录的综合结果是ps或经销商

城市和州都位于ps和经销商桌上

我已经尝试了所有的多个join,union以及我从这个堆栈溢出的所有东西。我实际上并没有得到我想要的东西,它真的会陷入地狱。

3 个答案:

答案 0 :(得分:1)

有些事情可能就是这样。

SELECT ad_id,ad_type,poster_id,cat,title,description,images,ad_status,
       IF (ad_type = 'ps', ps.name, dealer.name) as name,
       IF (ad_type = 'ps', ps.address, dealer.address) as address,
       IF (ad_type = 'ps', ps.city, dealer.city) as city,
       IF (ad_type = 'ps', ps.state, dealer.state) as state,
       IF (ad_type = 'ps', NULL, dealer.pack) as pack,
       IF (ad_type = 'ps', NULL, dealer.bussname) as bussname
FROM tbl_ads ads
LEFT JOIN tbl_ps ps ON (ps.ps_id = ads.poster_id)
LEFT JOIN tbl_dealer dealer ON (dealer.id = ads.poster_id)

然后搜索你可以使用这样的东西

SELECT ad_id,ad_type,poster_id,cat,title,description,images,ad_status,
       IF (ad_type = 'ps', ps.name, dealer.name) as _name,
       IF (ad_type = 'ps', ps.address, dealer.address) as _address,
       IF (ad_type = 'ps', ps.city, dealer.city) as _city,
       IF (ad_type = 'ps', ps.state, dealer.state) as _state,
       IF (ad_type = 'ps', NULL, dealer.pack) as _pack,
       IF (ad_type = 'ps', NULL, dealer.bussname) as _bussname
FROM tbl_ads ads
LEFT JOIN tbl_ps ps ON (ps.ps_id = ads.poster_id)
LEFT JOIN tbl_dealer dealer ON (dealer.id = ads.poster_id)
WHERE title LIKE '%tit%'
   OR description LIKE '%crip%'
HAVING _city LIKE '%Yorba%'
    OR _state LIKE '%Cali%'

检查此sqlFiddle

这是一个非常基本的搜索,搜索可以像匹配标题的匹配顺序一样强烈,例如标题中匹配的术语数量等。

答案 1 :(得分:1)

在这种情况下,最好使用CASE声明。

<强> SQLfiddle demo

SQL

SELECT ad_id, ad_type, title, description,
  case ad_type
    when 'ps'
      then (select city from tbl_ps where ps_id=poster_id)
    when 'dealer'
      then (select city from tbl_dealer where id=poster_id)
  end as CITY,
  case ad_type
    when 'ps'
      then (select state from tbl_ps where ps_id=poster_id)
    when 'dealer'
      then (select state from tbl_dealer where id=poster_id)
  end as STATE
from tbl_ads

答案 2 :(得分:0)

如果你有发布数据,那么你可以试试这个:

修改
表结构应该是:

tbl_ads ( ad_id, ad_type, poster_id,  cat,  title, description, images ... , ad_status )

tbl_ps (id, name , address, city, state )

tbl_dealer ( id, name , address, city, state, pack, bussname )

只需将ps_id重命名为id,然后重命名:

    $strTable = isset($_REQUEST['ads_type']) && $_REQUEST['ads_type']=="ps"  ? "tbl_ps" : "tbl_dealer";

     $sql = "SELECT * from  tbl_ads as t1 INNER JOIN ".$strTable." as t2 on t1.poster_id =  t2.id  ";
//execute your query

OLD:

首先,您需要检查广告类型:

If (isset($_REQUEST['ads_type'])){

$bolIsheader = $_REQUEST['ads_type'] == "ps" ? false : true;
if($bolIsheader)
$strSql = "
SELECT * from  tbl_ads as t1 INNER JOIN  
tbl_dealer as t2 on t1.poster_id= t2.id  ";
else
$strSql = "
SELECT * from  tbl_ads as t1 INNER JOIN  tbl_ps as t2 on t1.poster_id =  t2.ps_id ";
//next execute your query
}