0

我有一个名为展览的表格,其中包含:

  • 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_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 而没有其他人为什么会这样?

4

2 回答 2

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')
于 2013-01-28T11:27:55.520 回答
0

您需要为同一个表的多个连接提供区分引用。IE

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

然后使用这些引用来参考结果——您将从此类查询中获得的结果中了解我的意思。

于 2013-01-28T10:17:17.630 回答