在同一查询中多次从同一个表中进行选择

时间:2013-01-28 10:12:31

标签: php mysql codeigniter

我有一个名为exhibit的表,其中包含:

  • ex_id
  • ex_inv_id
  • ex_user_id
  • ex_pref_one
  • ex_pref_two
  • ex_pref_three
  • ex_pref_four
  • ex_terms_conditions
  • ex_pref_one_approved
  • ex_pref_two_approved
  • ex_pref_three_approved
  • ex_pref_four_approved

另一个名为stand的表包含:

  • stand_id
  • stand_no
  • stand_type

我现在需要将值放在数据表中,方法是从展示表中选择ex_pref_one / two / three / four columns = stand_id

我尝试使用连接但是我收到一条错误,表示表stand不存在 我正在使用codeigniter PHP框架

这是代码抱歉我没先发布

        $user_id = $this->session->userdata('user_id');
    if(!$user_id || $user_id == 0 )return 0;

    $where = array(
        'exhibit.ex_user_id'=>$user_id,
        );
    return $this->db->select('
        exhibit.*,
        S1.*,
        S2.*,
        S3.*,
        S4.*,
        ')
    ->join('stand AS S1', 'exhibit.ex_pref_one = S1.stand_id', 'LEFT')
    ->join('stand AS S2', 'exhibit.ex_pref_two = S2.stand_id', 'LEFT')
    ->join('stand AS S3', 'exhibit.ex_pref_three = S3.stand_id', 'LEFT')
    ->join('stand AS S4', 'exhibit.ex_pref_four = S4.stand_id', 'LEFT')
    ->where($where)
    ->from('exhibit')
    ->get()
    ->result();

$ this-> db-> last_query();

的结果
    SELECT `exhibit`.*,
       `S1`.*,
       `S2`.*,
       `S3`.*,
       `S4`.*
FROM (`exhibit`)
LEFT JOIN `stand` AS S1 ON `exhibit`.`ex_pref_one` = `S1`.`stand_id`
LEFT JOIN `stand` AS S2 ON `exhibit`.`ex_pref_two` = `S2`.`stand_id`
LEFT JOIN `stand` AS S3 ON `exhibit`.`ex_pref_three` = `S3`.`stand_id`
LEFT JOIN `stand` AS S4 ON `exhibit`.`ex_pref_four` = `S4`.`stand_id`
WHERE `exhibit`.`ex_user_id` = 1

结果的var_dump

array (size=1)

 0 => 

 object(stdClass)[25]
  public 'ex_id' => string '2' (length=1)
  public 'ex_inv_id' => string '2147483647' (length=10)
  public 'ex_user_id' => string '1' (length=1)
  public 'ex_pref_one' => string '6' (length=1)
  public 'ex_pref_two' => string '14' (length=2)
  public 'ex_pref_three' => string '13' (length=2)
  public 'ex_pref_four' => string '21' (length=2)
  public 'ex_terms_conditions' => string '1' (length=1)
  public 'ex_pref_one_approved' => string '0' (length=1)
  public 'ex_pref_two_approved' => string '0' (length=1)
  public 'ex_pref_three_approved' => string '0' (length=1)
  public 'ex_pref_four_approved' => string '0' (length=1)
  public 'stand_id' => string '21' (length=2)
  public 'stand_no' => string '20' (length=2)
  public 'stand_type' => string 'Gold' (length=4)

但它似乎只能拉出ex_pref_four而没有其他人为什么会这样?

2 个答案:

答案 0 :(得分:1)

首先为别名,您不需要AS。所以你可以这样做:

$this->db->select('
        exhibit.*,
        S1.*,
        S2.*,
        S3.*,
        S4.*,
        ')
    ->from('exhibit')
    ->join('stand S1', 'exhibit.ex_pref_one = S1.stand_id', 'LEFT')
    ->join('stand S2', 'exhibit.ex_pref_two = S2.stand_id', 'LEFT')
    ->join('stand S3', 'exhibit.ex_pref_three = S3.stand_id', 'LEFT')
    ->join('stand S4', 'exhibit.ex_pref_four = S4.stand_id', 'LEFT')
    ->where('whatever')

答案 1 :(得分:0)

您需要为同一个表提供多个连接作为区别参考。即

SELECT * FROM exhibit LEFT JOIN stand AS a ON exhibit.ex_pref_one = stand.stand_id LEFT JOIN stand AS b ON exhibit.ex_pref_two = stand.stand_id

然后使用这些引用参考结果 - 您将从这样的查询中获得的结果中看到我的意思。

相关问题