在一个查询中从具有不同列的不同表中进行选择

时间:2013-06-05 15:54:07

标签: php sql

我正在尝试从标题中描述的表中获取一些值,但我不知道如何,我已尝试过此但它只返回最后一个值(第二个选择的b.name ):

SELECT * 
FROM (
(SELECT a.priority,a.ticket_status,a.title, b.name ,c.department_name, null, null 
FROM ".$SupportTicketsTable." a, ".$SupportUserTable." b, ".$SupportDepaTable." c 
WHERE a.enc_id=? AND b.id=a.user_id AND c.id=a.department_id LIMIT 1) 
UNION 
(SELECT null,null,null,null,null, b.mail,b.name 
FROM ".$SupportTicketsTable." a, ".$SupportUserTable." b 
WHERE a.enc_id=? AND b.id=a.operator_id LIMIT 1)) AS t2

执行查询后:

$result = $stmt->bind_result($prio, $stat, $tit, $name, $dpname, $tomail, $opname);
while (mysqli_stmt_fetch($stmt)){
$mail=$tomail;
$rep=array(
    '{TICKET_OPERATOR_NAME}'=>$opname,
    '{TICKET_CREATOR_NAME}'=>$name,
    '{TICKET_PRIORITY}'=>$prio,
    '{TICKET_STATUS}'=>$stat,
    '{TICKET_DEPARTMENT}'=>$dpname,
    '{TICKET_URL}'=>dirname(dirname(curPageURL())).'user/view.php?'.$argv[2],
    '{TICKET_TITLE}'=>$tit);
}

结果如下:

Array
(
    [{TICKET_OPERATOR_NAME}] => Adelaide
    [{TICKET_CREATOR_NAME}] => 
    [{TICKET_PRIORITY}] => 
    [{TICKET_STATUS}] => 
    [{TICKET_DEPARTMENT}] => 
    [{TICKET_URL}] => http://razorphyn.com/products/supportuser/view.php?e4e0d6dac254c905862480f8a16273f7c3157b80db2c2ad7f03306376da85ef451adf0cc71ce60.98140917
    [{TICKET_TITLE}] => 
    [{SITE_NAME}] => Razorphyn
    [{SITE_ADDRESS}] => http://razorphyn.com
)

这应该是最终输出:

    Array
(
    [{TICKET_OPERATOR_NAME}] => Adelaide
    [{TICKET_CREATOR_NAME}] => Pippo
    [{TICKET_PRIORITY}] => 0
    [{TICKET_STATUS}] => 1
    [{TICKET_DEPARTMENT}] => Support
    [{TICKET_URL}] => http://razorphyn.com/products/supportuser/view.php?e4e0d6dac254c905862480f8a16273f7c3157b80db2c2ad7f03306376da85ef451adf0cc71ce60.98140917
    [{TICKET_TITLE}] => Test
    [{SITE_NAME}] => Razorphyn
    [{SITE_ADDRESS}] => http://razorphyn.com
)
$mail=maymail@myhost.com

表:

CREATE TABLE IF NOT EXISTS `razorphyn_support_list_tickets` (
`id`                BIGINT(15)  UNSIGNED        NOT NULL    AUTO_INCREMENT,
`enc_id`            CHAR(87),
`department_id`     BIGINT(11)  UNSIGNED        NOT NULL,
`operator_id`       BIGINT(11)  UNSIGNED        NOT NULL DEFAULT 0,
`user_id`           BIGINT(11)  UNSIGNED        NOT NULL,
`title`             VARCHAR(255)                NOT NULL,
`priority`          INT(2)      UNSIGNED        NOT NULL,
`website`           VARCHAR(200)                NOT NULL,
`contype`           ENUM('0','1','2','3','4','5')   NOT NULL    DEFAULT '0',
`ftp_user`          VARCHAR(60)                 NOT NULL,
`ftp_password`      VARCHAR(60)                 NOT NULL,
`created_time`      DATETIME                    NOT NULL,
`last_reply`        DATETIME                    NOT NULL,
`ticket_status`     ENUM('0','1','2')           NOT NULL    DEFAULT '2',
PRIMARY KEY (`id`),
UNIQUE KEY (`user_id`,`title`),
INDEX (`enc_id`,`department_id`,`operator_id`,`user_id`,`ticket_status`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=20;

CREATE TABLE IF NOT EXISTS `razorphyn_support_users` (
`id`                BIGINT(15)  UNSIGNED        NOT NULL AUTO_INCREMENT,
`name`              VARCHAR(50)                 NOT NULL,
`mail`              VARCHAR(50)                 NOT NULL,
`password`          VARCHAR(200)                NOT NULL,
`reg_key`           VARCHAR(260)                NOT NULL,
`tmp_password`      VARCHAR(31)                 NULL,
`ip_address`        VARCHAR(50)                 NOT NULL,
`status`            ENUM('0','1','2','3','4')   NOT NULL    DEFAULT '3',
`holiday`           ENUM('0','1')               NOT NULL    DEFAULT '0',
`mail_alert`        ENUM('no','yes')            NOT NULL    DEFAULT 'yes',
`assigned_tickets`  INT(5)  UNSIGNED            NOT NULL    DEFAULT 0,
`solved_tickets`    BIGINT(11)  UNSIGNED        NOT NULL    DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE KEY(`mail`),
INDEX (`name`,`mail`,`status`,`holiday`,`assigned_tickets`,`solved_tickets`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=55;

CREATE TABLE IF NOT EXISTS `razorphyn_support_departments` (
`id`                BIGINT(11)  UNSIGNED    NOT NULL    AUTO_INCREMENT,
`department_name`   VARCHAR(70)             NOT NULL,
`active`            ENUM('0','1')           NOT NULL    DEFAULT '1',
`public_view`       ENUM('0','1')           NOT NULL    DEFAULT '1',
PRIMARY KEY     (`id`),
UNIQUE KEY      (`department_name`),
INDEX (`id`,`department_name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=0;

提前致谢

2 个答案:

答案 0 :(得分:1)

也许这个:

SELECT 
a.priority,a.ticket_status ,a.title, b.name ,c.department_name,
tu.mail,tu.name 
FROM 
razorphyn_support_list_tickets a, 
razorphyn_support_users b, 
razorphyn_support_departments c,
razorphyn_support_list_tickets lt,
razorphyn_support_users tu
WHERE 
a.enc_id=? 
AND b.id=a.user_id 
AND c.id=a.department_id 
AND tu.id=lt.operator_id
LIMIT 1;

答案 1 :(得分:0)

不确定您使用的是哪个数据库(假设它是MySQL或Postgre),但您尝试过subselect:

SELECT a.priority,a.ticket_status,a.title, b.name ,c.department_name, d.mail, d.name 
FROM ".$SupportTicketsTable." a, ".$SupportUserTable." b, ".$SupportDepaTable." c,
     (SELECT b1.id, b1.mail,b1.name 
        FROM ".$SupportTicketsTable." a1, ".$SupportUserTable." b1
        WHERE b1.id=a1.user_id 
      ) d
   WHERE a.enc_id=? AND b.id=a.user_id AND c.id=a.department_id AND d.id = b.id LIMIT 1)  
) AS t2