아래에 mysql 쿼리가 있습니다. 나는 어느 카운터를 사용 했는가? 카테고리 ID 1을 3 번 입력하면 카운터가 3이 올 것입니다.하지만이 경우에는 다른 coloumn을 입력하지 않으면 NO가 필요합니다. 출력이 KU 및 전기에 mysql 쿼리에 카운터가 표시됩니다.
KU Electrical Yes 6 2 No 1 2내 판매 채널 이름입니다해야합니다. 예 (Yes)는 KU의 출입국 카운터와 진입하지 않은 수단을 의미합니다. 이것으로 도와주세요. 난 고민하고
select
SalesChannel.name,
Transaction.category_id,
count(Transaction.category_id) as "count"
from outlets Outlet
inner join transactions Transaction on Outlet.id = Transaction.outlet_id
inner join sale_channels SalesChannel on SalesChannel.id = Outlet.sale_channel_id
group by Transaction.category_id;
아래 제가
1) 트랜잭션
CREATE TABLE IF NOT EXISTS `transactions` (
`id` int(11) NOT NULL,
`zone_id` int(11) NOT NULL,
`state_id` int(11) NOT NULL,
`city_id` int(11) NOT NULL,
`category_id` int(11) NOT NULL,
`sub_category_id` int(11) NOT NULL,
`brand_id` int(11) NOT NULL,
`model_id` int(11) NOT NULL,
`outlet_id` int(11) NOT NULL,
`no_of_units` int(11) NOT NULL,
`mop` decimal(10,2) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `transactions`
--
INSERT INTO `transactions` (`id`, `zone_id`, `state_id`, `city_id`, `category_id`, `sub_category_id`, `brand_id`, `model_id`, `outlet_id`, `no_of_units`, `mop`) VALUES
(1, 2, 2, 2, 2, 1, 1, 1, 1, 3, '6.00'),
(2, 2, 2, 2, 2, 1, 1, 1, 1, 3, '6.00'),
(3, 1, 1, 1, 1, 1, 1, 1, 1, 4, '2.00'),
(4, 2, 2, 2, 1, 1, 1, 1, 2, 4, '2.00');
2) 출구
를CREATE TABLE IF NOT EXISTS `outlets` (
`id` int(11) NOT NULL,
`outlet_code` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`zone_id` int(11) NOT NULL,
`state_id` int(11) NOT NULL,
`city_id` int(11) NOT NULL,
`sale_channel_id` int(11) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `outlets`
--
INSERT INTO `outlets` (`id`, `outlet_code`, `name`, `zone_id`, `state_id`, `city_id`, `sale_channel_id`, `is_active`, `created`, `modified`) VALUES
(1, '1508', 'Ashok electricals', 2, 2, 2, 1, 1, '2016-10-03 00:00:00', '2016-10-03 00:00:00'),
(2, '1233', 'vinayak electricals', 1, 1, 1, 2, 1, '2016-10-04 00:00:00', '2016-10-04 00:00:00');
3) sale_chennals 사용되는 세 개의 테이블이다
CREATE TABLE IF NOT EXISTS `sale_channels` (
`id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
`is_active` tinyint(1) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `sale_channels`
--
INSERT INTO `sale_channels` (`id`, `name`, `is_active`, `created`, `modified`) VALUES
(1, 'KU', 1, '2016-10-03 00:00:00', '2016-10-03 00:00:00'),
(2, 'Electricals', 1, '2016-10-04 00:00:00', '2016-10-04 00:00:00');
,
SQL 구문 : http://sqlfiddle.com/#!9/3f497/1
당신이 여기 크로스를 추가하는 대신 내부 조인의 조인 왼쪽 Y 그런 다음 외부하려는 거래에 참여? 우리는 내부 조인을 사용하여 필요한 결과를 얻을 수 없습니까? \ – sukh
아니요. SQL 쿼리는 해당 내용 만 표시 할 수 있습니다. Electricals/category 2의 조합에 대한 기록이 없으면 표시해야합니다. 이것이 교차 결합이하는 것입니다. 당신은 질문을 시도 했습니까? 결과의 차이점을 보시겠습니까? 결과에 만족하십니까? –
그것 판매 채널 이름을 여러 번주는. 카테고리 ID가 6 회 추가 될 경우 KU가 6 회 있어야합니다. 여러 개의 카운터가있을 수 있습니다. – sukh