0

これがテーブルと私の現在のコードです

http://www.sqlfiddle.com/#!2/f1754/6

そのままでは動作しますが、リソースを大量に消費するような気がします...何かアイデアはありますか?

「ネット」の周りの他の人は、「副選択」は眉をひそめていると言いました...

(データベースを作成するためのコード)

CREATE TABLE IF NOT EXISTS `thread_classifieds` (
  `threadid` int(10) NOT NULL,
  `price` decimal(10,2) unsigned DEFAULT NULL,
  `obo` int(5) NOT NULL,
  `offer` int(5) NOT NULL,
  `state` varchar(100) NOT NULL,
  `location` varchar(100) NOT NULL,
  `area` varchar(150) NOT NULL,
  `posttype` varchar(50) NOT NULL,
  `posttype_firearm` varchar(50) NOT NULL,
  `caliber` varchar(100) NOT NULL,
  `manufacturer` varchar(100) NOT NULL,
  `action` varchar(50) NOT NULL,
  `type` varchar(50) NOT NULL,
  UNIQUE KEY `threadid` (`threadid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `thread_classifieds` (`threadid`, `price`, `obo`, `offer`, `state`, `location`, `area`, `posttype`,     `posttype_firearm`, `caliber`, `manufacturer`, `action`, `type`) VALUES
(72, 750.00, 1, 0, 'Kentucky', 'louisville', 'Okolona', 'for-sale', 'handguns', '9mm-luger', 'springfield', 'double-single-action', 'pistol');

CREATE TABLE IF NOT EXISTS `thread_classifieds_action` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `type` int(10) NOT NULL,
  `short` varchar(100) NOT NULL,
  `long` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;

INSERT INTO `thread_classifieds_action` (`id`, `type`, `short`, `long`) VALUES
(2, 2, 'double-single-action', 'Double / Single Action');

CREATE TABLE IF NOT EXISTS `thread_classifieds_caliber` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `type` int(10) NOT NULL,
  `short` varchar(100) NOT NULL,
  `long` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=211 ;

INSERT INTO `thread_classifieds_caliber` (`id`, `type`, `short`, `long`) VALUES
(46, 2, '9mm-luger', '9mm Luger');

CREATE TABLE IF NOT EXISTS `thread_classifieds_locations` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `forumid` int(10) NOT NULL,
  `short` varchar(100) NOT NULL,
  `long` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=242 ;

INSERT INTO `thread_classifieds_locations` (`id`, `forumid`, `short`, `long`) VALUES
(92, 34, 'louisville', 'Louisville');

CREATE TABLE IF NOT EXISTS `thread_classifieds_manufacturer` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `type` int(10) NOT NULL,
  `short` varchar(100) NOT NULL,
  `long` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=222 ;

INSERT INTO `thread_classifieds_manufacturer` (`id`, `type`, `short`, `long`) VALUES
(67, 2, 'springfield', 'Springfield');

CREATE TABLE IF NOT EXISTS `thread_classifieds_posttype` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `short` varchar(100) NOT NULL,
  `long` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `short` (`short`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

INSERT INTO `thread_classifieds_posttype` (`id`, `short`, `long`) VALUES
(2, 'for-sale', 'For Sale');

CREATE TABLE IF NOT EXISTS `thread_classifieds_posttype_firearm` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `short` varchar(100) NOT NULL,
  `long` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;

INSERT INTO `thread_classifieds_posttype_firearm` (`id`, `short`, `long`) VALUES
(2, 'handguns', 'Handguns');

CREATE TABLE IF NOT EXISTS `thread_classifieds_type` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `type` int(10) NOT NULL,
  `short` varchar(100) NOT NULL,
  `long` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

INSERT INTO `thread_classifieds_type` (`id`, `type`, `short`, `long`) VALUES
(2, 2, 'pistol', 'Pistol');

(これがクエリです)

SELECT 
`price`,    
`obo`,    
`offer`,    
`state`,  
`location` AS `location_org`,  
(SELECT `long` FROM `thread_classifieds_locations` WHERE `short` = `thread_classifieds`.`location` &&     `thread_classifieds`.`threadid` = 72) AS `location_full`,     
`area`,    
(SELECT `long` FROM `thread_classifieds_posttype` WHERE `short` = `thread_classifieds`.`posttype` &&     `thread_classifieds`.`threadid` = 72) AS `posttype`, 
(SELECT `long` FROM `thread_classifieds_posttype_firearm` WHERE `short` = `thread_classifieds`.`posttype_firearm` && `thread_classifieds`.`threadid` = 72) AS `posttype_firearm`, 
(SELECT `id` FROM `thread_classifieds_posttype_firearm` WHERE `short` = `thread_classifieds`.`posttype_firearm` && `thread_classifieds`.`threadid` = 72) AS `typenum`,                                 
(SELECT `long` FROM `thread_classifieds_caliber` WHERE `short` = `thread_classifieds`.`caliber` && `thread_classifieds`.`threadid` = 72 && `thread_classifieds_caliber`.`type` = `typenum`) AS `caliber`,    
(SELECT `long` FROM `thread_classifieds_manufacturer` WHERE `short` = `thread_classifieds`.`manufacturer` && `thread_classifieds`.`threadid` = 72 && `thread_classifieds_manufacturer`.`type` = `typenum`) AS `manufacturer`,   
(SELECT `long` FROM `thread_classifieds_action` WHERE `short` = `thread_classifieds`.`action` && `thread_classifieds`.`threadid` = 72 && `thread_classifieds_action`.`type` = `typenum`) AS `action`,    
(SELECT `long` FROM `thread_classifieds_type` WHERE `short` = `thread_classifieds`.`type` && `thread_classifieds`.`threadid` = 72 && `thread_classifieds_type`.`type` = `typenum`) AS `type`    
FROM `thread_classifieds`    
WHERE `threadid` = 72 LIMIT 1
4

3 に答える 3

3

はい、結合を使用できます。ここで「目を出血させる」クエリを繰り返すつもりはありませんが、基本的には次のようになります。

SELECT x, y, (SELECT z FROM othertable WHERE blah blah)

参加として、それは

SELECT x, y, sometable.z
FROM sometable
JOIN othertable ON sometable.AAA = othertable.BBB

ここで、aaa / bbbは、2つのテーブルを相互に関連付けるフィールドです。


おそらくこのようなもの:

SELECT ..., thread_classifeds_locations.long AS location_full
FROM thread_classifieds
LEFT JOIN thread_classifieds_locations ON thread_classifieds.long = thread_classifieds_locations.long
...
WHERE (thread_classifieds.threadid= 72) AND (thread_classifieds.location = 'short')
于 2013-02-11T18:59:45.673 に答える
2

サブクエリを結合に置き換える必要があります。

select c.`price`,    
  c.`obo`,    
  c.`offer`,    
  c.`state`,  
  c.`location` AS `location_org`,
  l.`long` AS `location_full`,
  c.`area`,
  p.`long` AS `posttype`,
  pf.`long` AS `posttype_firearm`,
  pf.id AS `typenum`,
  cc.`long`  AS `caliber`,
  m.`long`  AS `manufacturer`,
  a.`long`  AS `action`,
  t.`long`  AS `type` 
from thread_classifieds c
left join thread_classifieds_locations l
  on c.location = l.short
left join thread_classifieds_posttype p
  on c.posttype = p.short
left join thread_classifieds_posttype_firearm pf
  on c.posttype_firearm = pf.short
left join thread_classifieds_caliber cc
  on c.caliber = cc.short
  and pf.id = cc.`type`
left join thread_classifieds_manufacturer m
  on c.manufacturer = m.short
  and pf.id = m.`type`
left join thread_classifieds_action a
  on c.action = a.short
  and pf.id = a.`type`
left join thread_classifieds_type t
  on c.type = t.short
  and pf.id = t.`type`
where c.`threadid` = 72 LIMIT 1;

SQL FiddlewithDemoを参照してください

于 2013-02-11T19:17:39.270 に答える
0

代わりに、最初の選択を結合としてやり直しました。始めるべき

SELECT 
`price`,    
`obo`,    
`offer`,    
`state`,  
`location` AS `location_org`,
`posttype`.`long`,
(SELECT `long` FROM `thread_classifieds_locations` WHERE `short` =`thread_classifieds`.`location` && `thread_classifieds`.`threadid` = 72) AS  `location_full`,     
 `area`,    
 (SELECT `long` FROM `thread_classifieds_posttype_firearm` WHERE `short` =  `thread_classifieds`.`posttype_firearm` && `thread_classifieds`.`threadid` = 72) AS `posttype_firearm`, 
 (SELECT `id` FROM `thread_classifieds_posttype_firearm` WHERE `short` = `thread_classifieds`.`posttype_firearm` && `thread_classifieds`.`threadid` = 72) AS `typenum`,                                 
 (SELECT `long` FROM `thread_classifieds_caliber` WHERE `short` = `thread_classifieds`.`caliber` && `thread_classifieds`.`threadid` = 72 && `thread_classifieds_caliber`.`type` = `typenum`) AS `caliber`,    
 (SELECT `long` FROM `thread_classifieds_manufacturer` WHERE `short` = `thread_classifieds`.`manufacturer` && `thread_classifieds`.`threadid` = 72 && `thread_classifieds_manufacturer`.`type` = `typenum`) AS `manufacturer`,   
 (SELECT `long` FROM `thread_classifieds_action` WHERE `short` = `thread_classifieds`.`action` && `thread_classifieds`.`threadid` = 72 && `thread_classifieds_action`.`type` = `typenum`) AS `action`,    
 (SELECT `long` FROM `thread_classifieds_type` WHERE `short` = `thread_classifieds`.`type` && `thread_classifieds`.`threadid` = 72 && `thread_classifieds_type`.`type` = `typenum`) AS `type`    
 FROM `thread_classifieds` AS `thread_classifieds`
INNER JOIN `thread_classifieds_posttype` AS `posttype` ON  `posttype`.`short` = `thread_classifieds`.`posttype` && `thread_classifieds`.`threadid` = 72
WHERE `threadid` = 72 LIMIT 1
于 2013-02-11T19:11:53.623 に答える