sql多对多(?)pdo查询需要帮助

时间:2018-03-08 17:59:44

标签: php sql pdo

我正在尝试连接两个表后返回行,我很难得到我想要的结果。我有两张桌子:

CREATE TABLE `all_sessions` (
    `session_id` int(11) NOT NULL AUTO_INCREMENT,
    `type` varchar(255) NOT NULL DEFAULT 'TBA',
    `breakoutsessions` varchar(255) NOT NULL DEFAULT '0',
    `location` varchar(255) NOT NULL DEFAULT 'TBD',
    `title` varchar(255) NOT NULL DEFAULT 'Title',
    `details` varchar(255) NOT NULL DEFAULT 'TBA',
  PRIMARY KEY (`session_id`),
  UNIQUE KEY `session_id_UNIQUE` (`session_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

session_id  type  breakoutsessions  location  title  details
----------  ----  ----------------  --------  -----  -----------------
1           lect  1,3,4             bld1      ph1    details text here
2           tour  2                 bld15     ph2    details text here
3           soci  6                 bld3      ph3    details text here          
4           lect  1,2               bld2      ph3    details text here
5           lect  6                 bld3      ph3    details text here

AND

CREATE TABLE `break_sess_pair` (
    `pri_key` int(11) NOT NULL AUTO_INCREMENT,
    `session_id` varchar(255) NOT NULL,
    `breakout_id` varchar(255) NOT NULL,
  PRIMARY KEY (`pri_key`),
  UNIQUE KEY `pri_key_UNIQUE` (`pri_key`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

pri_key  session_id  breakout_id
-------  ----------  -----------
1        1           1          
2        1           3
3        1           4          
4        2           2
5        3           6          
6        4           1
7        4           2          
8        5           6

我尝试简单地加入表格,但这似乎不起作用,我最终得到了空值。这是在session_id和breakout_id

之外的所有字段中返回空值的代码
function manytable(){
  $dbcon = dbConnect::getInstance();
  $stmt = $dbcon->db->prepare("
    SELECT
      *
    FROM `all_sessions`
    RIGHT JOIN `break_sess_pair`
    ON 'break_sess_pair.session_id'='all_sessions.session_id'
  ");
  $comp_sessions_list = array();
  if($stmt->execute()){
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $result;
  }
}

我想要做的是将两者结合起来,以便最终得到类似的东西:

session_id  type  location  title  details            breakout_session
----------  ----  --------  -----  -----------------  ----------------
1           lect  bld1      ph1    details text here  1
1           lect  bld1      ph1    details text here  3
1           lect  bld1      ph1    details text here  4
2           tour  bld15     ph2    details text here  2
3           soci  bld3      ph3    details text here  6        
4           lect  bld2      ph3    details text here  1
4           lect  bld2      ph3    details text here  2
5           lect  bld3      ph3    details text here  6

2 个答案:

答案 0 :(得分:1)

SELECT b.session_id, 
a.type, 
a.location, 
a.title, 
a.details, 
b.breakout_session
FROM `all_sessions` as a
INNER JOIN `break_sess_pair` as b
ON 'break_sess_pair.session_id'='all_sessions.session_id'

这应该有效

答案 1 :(得分:0)

对于任何感兴趣的人,最终的工作职能是:

function manytable(){
  $dbcon = dbConnect::getInstance();
  $stmt = $dbcon->db->prepare("
    SELECT 
      b.session_id,
      a.type,
      a.location,
      a.title,
      a.details,
      b.breakout_id
    FROM `all_sessions` AS a
    INNER JOIN `break_sess_pair` AS b
    ON b.session_id = a.session_id
  ");
  if($stmt->execute()){
    $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $result;
  }
}