Domanda

For my project I have a strange requirement!!

I am having three tables such as ATab, BTab, and CTab as followes

CREATE TABLE IF NOT EXISTS `ATab` (
`id` int(8) unsigned NOT NULL AUTO_INCREMENT,
`oid` int(8) unsigned NOT NULL,
`type` int(1) unsigned NOT NULL ,
`parent` int(8) unsigned NOT NULL ,
`title` varchar(128) CHARACTER SET utf8 NOT NULL DEFAULT '' ,
`tagname` varchar(16) CHARACTER SET utf8 NOT NULL DEFAULT '',
`uid` int(8) NOT NULL COMMENT 'uid',
`name` varchar(64) CHARACTER SET utf8 DEFAULT NULL ,
`ctime` int(8) NOT NULL ,
`utime` int(11) DEFAULT NULL ,
`lnk` varchar(64) DEFAULT NULL ,
 PRIMARY KEY (`id`),
 KEY `re` (`oid`),
 KEY `re_type` (`type`),
 KEY `rg_parent` (`parent`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `BTab` (
`pid` int(8) unsigned NOT NULL AUTO_INCREMENT,
`ptype` int(1) DEFAULT NULL ,
`pname` varchar(128) CHARACTER SET utf8 DEFAULT NULL,
 PRIMARY KEY (`pid`),
 KEY `ptype` (`ptype`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `CTab` (
`cid` int(11) unsigned NOT NULL AUTO_INCREMENT,
`id` int(11) DEFAULT NULL,
`mvl` text CHARACTER SET utf8,
 PRIMARY KEY (`cid`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Sample entries:

INSERT INTO `ATab` (`id`, `oid`, `type`, `parent`, `title`, `tagname`, `uid`, `name`, `ctime`, `utime`, `lnk`) VALUES
(1, 1, 1, 1, 'child1', 'test', 1, 'john', 1383740369, 1383740369, '1'),
(2, 1, 2, 1, 'child2', 'test', 1, 'john', 1383740379, 1383740379, '1'),
(3, 2, 1, 2, 'child3', 'sample', 2, 'jose', 1383740389, 1383740399, '2'),
(4, 2, 2, 2, 'child4', 'sample', 2, 'jose', 1383740479, 1383740479, '2');

INSERT INTO `BTab` (`pid`, `ptype`, `pname`) VALUES
(1, 1, 'parent1'),
(2, 2, 'parent2');


INSERT INTO `CTab` (`cid`, `id`, `mvl`) VALUES
(1, 1, '{\r\n    "test": "test json data "\r\n}'),
(2, 2, '{\r\n    "sample": "sample json data"\r\n}');

BTab contains the parent entries. ATab contains the child entries for the entries in BTab. There will be multiple childs for parents. CTab contains data related to ATab (CTab.id = ATab.id). There will not be entries in CTab for all entries in the ATab(So I used LEFT OUTER JOIN).

My SELECT query:-

SELECT CONCAT(  "{\"items\":[", CONVERT( GROUP_CONCAT(  "{\"t", 
TYPE ,  "\":{\"id\":", ATab.id,  ",\"oid\":", oid,  ",\"ty\":", 
TYPE ,  ",\"p\":", parent,  ", \"rtnm\":\"", tagname,  "\",\"tl\":\"", title,  "\", \"nm\":\"", name,  "\", \"ct\":", ctime,  ", \"ut\":", utime,  ", \"u\":", uid,  " , \"mvl\":", IFNULL( CTab.mvl,  "{}" ) ,  ",\"lnk\":\"", IFNULL( lnk,  "0" ) ,  "\"}}" ) 
USING utf8 ) ,  "]}" ) AS jsn
FROM ATab
JOIN BTab
LEFT OUTER JOIN CTab ON CTab.id = ATab.id
WHERE oid =1
AND ATab.parent = BTab.pid
AND ATab.parent
IN ( 1 ) 
GROUP BY ATab.parent
ORDER BY utime DESC 
LIMIT 0 , 500

The output will be a JSON as

{
    "items": [
        {
            "t1": {
                "id": 1,
                "oid": 1,
                "ty": 1,
                "p": 1,
                "rtnm": "test",
                "tl": "child1",
                "nm": "john",
                "ct": 1383740369,
                "ut": 1383740369,
                "u": 1,
                "mvl": {
                    "test": "test json data "
                },
                "lnk": "1"
            }
        },
        {
            "t2": {
                "id": 2,
                "oid": 1,
                "ty": 2,
                "p": 1,
                "rtnm": "test",
                "tl": "child2",
                "nm": "john",
                "ct": 1383740379,
                "ut": 1383740379,
                "u": 1,
                "mvl": {
                    "sample": "sample json data"
                },
                "lnk": "1"
            }
        }
    ]
}

I need to sort the result based on the utime (ORDER BY utime DESC ). But the output is not sorted. I think the problem is related to the CONCAT statement I used( I am not sure about this). I cannot change the JSON structure. And I want the database to handle the sorting. The query is working fine.

Any help will be highly appreciated...

Edit:

Sorry folks..I try to create a sqlfiddle url. But I am not getting sqlfiddle site..May be network problem :(

Sqlfiddle url: http://sqlfiddle.com/#!2/3564f/1

È stato utile?

Soluzione

As sled mentions in the comments section - move your ORDER BY inside your GROUP_CONCAT. Hence:

SELECT CONCAT(  "{\"items\":[", CONVERT( GROUP_CONCAT(  "{\"t", 
TYPE ,  "\":{\"id\":", ATab.id,  ",\"oid\":", oid,  ",\"ty\":", 
TYPE ,  ",\"p\":", parent,  ", \"rtnm\":\"", tagname,  "\",\"tl\":\"", title,  "\", \"nm\":\"", name,  "\", \"ct\":", ctime,  ", \"ut\":", utime,  ", \"u\":", uid,  " , \"mvl\":", IFNULL( CTab.mvl,  "{}" ) ,  ",\"lnk\":\"", IFNULL( lnk,  "0" ) ,  "\"}}" ORDER BY utime DESC ) 
USING utf8 ) ,  "]}" ) AS jsn
FROM ATab
JOIN BTab
LEFT OUTER JOIN CTab ON CTab.id = ATab.id
WHERE oid =1
AND ATab.parent = BTab.pid
AND ATab.parent
IN ( 1 ) 
GROUP BY ATab.parent
LIMIT 0 , 500;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top