开发者

mysql concat sub query results as a subquery itself

开发者 https://www.devze.com 2023-03-28 20:44 出处:网络
this is a bit tough, i guess. what i\'m trying to achieve is to generate a string via group_concat and then run it as a query. my query return the

this is a bit tough, i guess.

what i'm trying to achieve is to generate a string via group_concat and then run it as a query. my query return the numCustomers as a query i would like to run.

is it possible to run this as a query inside the main query?

my query is as follows:

SELECT 
lastSent,tblmailinglistgroups.addDate,title, tblmailinglistgroups.active, tblmailinglistgroups.id,groupType,
CASE 
WHEN (groupType='s') THEN (SELECT COUNT(id) FROM tblmailinglistgroupscusts WHERE groupID=tblmailinglistgroups.id) 
WHEN (groupType='d') THEN (
CONCAT('SELECT COUNT(tblcustomers.id) FROM tblcustomers INNER JOIN tblcustomersextension ON tblcustomers.id=tblcustomersextension.customerID WHERE 1=1 ',
(SELECT GROUP_CONCAT(filterAndOr,' ',openBrackets,' ',filterBy, ' ',(
SELECT 
CASE 
WHEN filterOperator='ne' THEN '!=' WHEN filterOperator='e' THEN '=' WHEN filterOperator='be' THEN '>=' WHEN filterOperator='se' THEN '<=' WHEN filterOperator='b' THEN '>' WHEN filterOperator='s' THEN '<' WHEN filterOperator='in' THEN 'in' WHEN filterOperator='ni' THEN 'not in' WHEN 'nu' THEN 'IS NULL' 
END AS operator),' \'',filterValue,'\' ',closeBrackets SEPARATOR ' ') AS x 
FROM `tblmailinglistgroupsrules1`
WHERE groupID=tblmailinglistgroups.id
)
)
) END AS numCustomers
FROM 
tblmailinglistgroups 
LEFT JOIN tblmailinglistgroupscusts ON (tblmailinglistgroupscusts.groupID = tblmailinglistgroups.id) 
GROUP BY tblmailinglistgroups.id 
ORDER BY tblmailinglistgroups.id DESC

the table structes are:

CREATE TABLE IF NOT EXISTS `tblmailinglistgroups` (
`id` bigint(12) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL DEFAULT '',
`addDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`active` enum('on','off') NOT NULL DEFAULT 'on',
`lastSent` date NOT NULL DEFAULT '0000-00-00',
`groupType` enum('s','d') NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=67 ;


CREATE TABLE IF NOT EXISTS `tblmailinglistgro开发者_运维问答upscusts` (
`id` bigint(12) NOT NULL AUTO_INCREMENT,
`groupID` bigint(12) NOT NULL DEFAULT '0',
`custID` int(5) NOT NULL,
`email` varchar(200) NOT NULL DEFAULT '',
`phone` varchar(20) NOT NULL,
`comments` varchar(250) NOT NULL,
`addDate` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
KEY `groupID` (`groupID`),
KEY `custID` (`custID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=7813 ;


CREATE TABLE IF NOT EXISTS `tblmailinglistgroupsrules1` (
`id` int(5) NOT NULL AUTO_INCREMENT,
`groupID` int(5) NOT NULL,
`filterAndOr` enum('AND','OR') NOT NULL,
`openBrackets` enum('','(','((','(((','((((') NOT NULL,
`filterBy` varchar(40) NOT NULL,
`filterOperator` varchar(40) NOT NULL,
`filterValue` varchar(40) NOT NULL,
`closeBrackets` enum('',')','))',')))','))))') NOT NULL,
`showOrder` int(5) NOT NULL,
`addDate` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=415 ;


You could use stored procedures to execute a query from the concat. There is no way to do this inside a simple SQL Query.

A good explanation could be found here: http://www.it-iss.com/mysql/mysql-stored-procedures-and-dynamic-sql/

DANGER: If this is a web-application, it may be possible for attackers to manipuate the result of the concat statement to execute own, bad statements like DROP DATABASE.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号