0

我有这两个查询,我想将它们合并为一个,但我不确定如何去做。

查询一:

$query = "SELECT * FROM ".$db_tbl_comics." WHERE ".$db_fld_comics_publisher."='".$pub_id."'
AND ".$db_fld_comics_active."='1' GROUP BY ".$db_fld_comics_arc;

查询 2:

$q2 = mysql_query('SELECT '.$db_fld_arcs_title.' FROM '.$db_tbl_arcs.'
WHERE '.$db_fld_arcs_id.'="'.$result[$db_fld_comics_arc].'"');

漫画表:

CREATE TABLE IF NOT EXISTS `comics` (
  `id` varchar(255) NOT NULL,
  `arc` int(255) NOT NULL,
  `title` varchar(255) NOT NULL,
  `issue` decimal(5,1) DEFAULT NULL,
  `price` decimal(10,2) NOT NULL,
  `plot` longtext NOT NULL,
  `publisher` int(255) NOT NULL,
  `isbn` varchar(255) NOT NULL,
  `published` date NOT NULL,
  `cover` varchar(255) NOT NULL DEFAULT './images/nopic.jpg',
  `added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `views` int(255) NOT NULL DEFAULT '0',
  `active` int(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `arc` (`arc`,`title`,`issue`,`publisher`)
);

弧表:

CREATE TABLE IF NOT EXISTS `arcs` (
  `id` int(255) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL,
  `plot` longtext NOT NULL,
  `added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `title` (`title`)
);

我需要做的是从相应漫画弧的弧表中获取弧标题。

4

2 回答 2

1

很简单(将 2 个查询合二为一,仅选择必填字段并使用IN):

SELECT
    '.$db_fld_arcs_title.'
    FROM '.$db_tbl_arcs.'
    WHERE '.$db_fld_arcs_id.' IN (
        SELECT '.$db_fld_comics_arc.'
            FROM '.$db_tbl_comics.' 
            WHERE '.$db_fld_comics_publisher.'='".$pub_id."'
            AND '.$db_fld_comics_active.'='1' GROUP BY '.$db_fld_comics_arc.'
    )
于 2012-08-22T05:28:06.763 回答
1

您需要使用INNER JOIN它,因为我认为两个表上都存在记录。

SELECT  a.*, b.title
FROM    comics a INNER JOIN arcs b
            on a.id = b.id
WHERE   a.Title = 'VALUEHERE'

显示comics表格中的所有详细信息和弧的标题

于 2012-08-22T05:30:28.247 回答