mysql group_concat on multiple tables - mysql

This is the first time I ever use stackoverflow,.. be gentle on me ;)
I have little problem here with getting duplicated results from GROUP_CONCAT when using more than one JOIN on the map tables.
It is not easy to explain this, but I will try:
I have created a SQLFiddle for testing: http://sqlfiddle.com/#!9/d2b347/3
I want the query to be just one instead of 1 for all of the posts and then hammering on every test. But since the GROUP_CONCAT is merging those test results I am getting twice as much data than I want.
It is possible somehow to make the query more reliable. To always have the GROUP_CONCAT to be the exact numbers of tests?
I expect/want the output to be:
|---------|-----------------|------------|---------|-------------|
| post_id | flows | flow_types | powers | power_types |
|---------|-----------------|------------|---------|-------------|
| 1 | 100,140 | a,b | 1,1 | a,b |
|---------|-----------------|------------|---------|-------------|
| 2 | 200,200,200 | a,b,c | (null) | (null) |
|---------|-----------------|------------|---------|-------------|
but it is:
|---------|-----------------|------------|---------|-------------|
| post_id | flows | flow_types | powers | power_types |
|---------|-----------------|------------|---------|-------------|
| 1 | 100,100,140,140 | a,a,b,b | 1,1,1,1 | a,b,a,b |
|---------|-----------------|------------|---------|-------------|
| 2 | 200,200,200 | a,b,c | (null) | (null) |
|---------|-----------------|------------|---------|-------------|
and with GROUP_CONCAT DISTINCT I get:
|---------|-----------------|------------|---------|-------------|
| post_id | flows | flow_types | powers | power_types |
|---------|-----------------|------------|---------|-------------|
| 1 | 100,140 | a,b | 1 | a,b |
|---------|-----------------|------------|---------|-------------|
| 2 | 200 | a,b,c | (null) | (null) |
|---------|-----------------|------------|---------|-------------|
Here is the create query:
DROP TABLE IF EXISTS `posts`;
CREATE TABLE IF NOT EXISTS `posts` (
`post_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`post` varchar(256) CHARACTER SET ascii NOT NULL,
PRIMARY KEY (`post_id`),
UNIQUE KEY `UNQ_post` (`post`) USING HASH
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `posts_test1`;
CREATE TABLE IF NOT EXISTS `posts_test1` (
`post_id` bigint(20) unsigned NOT NULL,
`test1_id` bigint(20) unsigned NOT NULL,
`type_id` int(10) unsigned NOT NULL DEFAULT 1,
PRIMARY KEY (`post_id`,`test1_id`,`type_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `test1`;
CREATE TABLE IF NOT EXISTS `test1` (
`test1_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`flow` int(10) unsigned NOT NULL,
PRIMARY KEY (`test1_id`),
KEY `IDX_FLOW` (`flow`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `posts_test2`;
CREATE TABLE IF NOT EXISTS `posts_test2` (
`post_id` bigint(20) unsigned NOT NULL,
`test2_id` bigint(20) unsigned NOT NULL,
`type_id` int(10) unsigned NOT NULL DEFAULT 1,
PRIMARY KEY (`post_id`,`test2_id`,`type_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `test2`;
CREATE TABLE IF NOT EXISTS `test2` (
`test2_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`power` int(10) unsigned NOT NULL,
PRIMARY KEY (`test2_id`),
KEY `IDX_POWER` (`power`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `types`;
CREATE TABLE IF NOT EXISTS `types` (
`type_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(50) CHARACTER SET ascii DEFAULT NULL,
PRIMARY KEY (`type_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `types` (`type_id`, `type`) VALUES
(1, 'a'),
(2, 'b'),
(3, 'c');
INSERT INTO `posts` (`post_id`, `post`) VALUES
(1, 'test1'),
(2, 'test2');
INSERT INTO `test1` (`test1_id`, `flow`) VALUES
(1, 100),
(2, 140),
(3, 200),
(4, 200),
(5, 200);
INSERT INTO `posts_test1` (`post_id`, `test1_id`, `type_id`) VALUES
(1, 1, 1),
(1, 2, 2),
(2, 3, 1),
(2, 4, 2),
(2, 5, 3);
INSERT INTO `test2` (`test2_id`, `power`) VALUES
(1, 1),
(2, 1);
INSERT INTO `posts_test2` (`post_id`, `test2_id`, `type_id`) VALUES
(1, 1, 1),
(1, 2, 2);
And here are my select queries..
SELECT
p.post_id, p.post,
GROUP_CONCAT(t1.flow) flow,
GROUP_CONCAT(t1t.type) flow_types
FROM posts p
LEFT JOIN posts_test1 pt1 USING (post_id)
LEFT JOIN test1 t1 USING (test1_id)
LEFT JOIN types t1t ON (t1t.type_id = pt1.type_id)
GROUP BY p.post_id; # works fine
SELECT
p.post_id, p.post,
GROUP_CONCAT(t2.power) powers,
GROUP_CONCAT(t2t.type) power_types
FROM posts p
LEFT JOIN posts_test2 pt2 USING (post_id)
LEFT JOIN test2 t2 USING (test2_id)
LEFT JOIN types t2t ON (t2t.type_id = pt2.type_id)
GROUP BY p.post_id; # works fine
SELECT
p.post_id, p.post,
GROUP_CONCAT(t1.flow) flow,
GROUP_CONCAT(t1t.type) flow_types,
GROUP_CONCAT(t2.power) powers,
GROUP_CONCAT(t2t.type) power_types
FROM posts p
LEFT JOIN posts_test1 pt1 USING (post_id)
LEFT JOIN test1 t1 USING (test1_id)
LEFT JOIN types t1t ON (t1t.type_id = pt1.type_id)
LEFT JOIN posts_test2 pt2 USING (post_id)
LEFT JOIN test2 t2 USING (test2_id)
LEFT JOIN types t2t ON (t2t.type_id = pt2.type_id)
GROUP BY p.post_id; # getting duplicated GROUP_CONCAT results
SELECT
p.post_id, p.post,
GROUP_CONCAT(DISTINCT t1.flow) flow,
GROUP_CONCAT(DISTINCT t1t.type) flow_types,
GROUP_CONCAT(DISTINCT t2.power) powers,
GROUP_CONCAT(DISTINCT t2t.type) power_types
FROM posts p
LEFT JOIN posts_test1 pt1 USING (post_id)
LEFT JOIN test1 t1 USING (test1_id)
LEFT JOIN types t1t ON (t1t.type_id = pt1.type_id)
LEFT JOIN posts_test2 pt2 USING (post_id)
LEFT JOIN test2 t2 USING (test2_id)
LEFT JOIN types t2t ON (t2t.type_id = pt2.type_id)
GROUP BY p.post_id; # DISTINCT wipes the GROUP_CONCAT if same result...
Thanks and have a nice day!!
edit: added expected result as suggest, thanks :)

The issue here is that there are two different junction tables (and two different connection chains), originating from a single table post. So a linear JOIN chain does not work. Duplicates in one of the junction table leads to duplication in other chain, when linear joining is done.
One way is to consider these two different JOIN chains in two separate Derived Tables (subqueries inside the FROM clause), and determine their respective grouped/aggregated expressions. We can then JOIN back these two chains using post_id.
Query
SELECT
dt1.post_id,
dt1.flows,
dt1.flow_types,
dt2.powers,
dt2.power_types
FROM
(
SELECT
p.post_id,
GROUP_CONCAT(t1.flow) AS flows,
GROUP_CONCAT(typ.type) AS flow_types
FROM posts p
LEFT JOIN posts_test1 pt1
ON pt1.post_id = p.post_id
LEFT JOIN test1 t1
ON t1.test1_id = pt1.test1_id
LEFT JOIN types typ
ON typ.type_id = pt1.type_id
GROUP BY p.post_id
) AS dt1
JOIN
(
SELECT
p.post_id,
GROUP_CONCAT(t2.power) AS powers,
GROUP_CONCAT(typ.type) AS power_types
FROM posts p
LEFT JOIN posts_test2 pt2
ON pt2.post_id = p.post_id
LEFT JOIN test2 t2
ON t2.test2_id = pt2.test2_id
LEFT JOIN types typ
ON typ.type_id = pt2.type_id
GROUP BY p.post_id
) AS dt2
ON dt1.post_id = dt2.post_id;
Result
| post_id | flows | flow_types | powers | power_types |
| ------- | ----------- | ---------- | ------ | ----------- |
| 1 | 100,140 | a,b | 1,1 | a,b |
| 2 | 200,200,200 | a,b,c | | |
View on DB Fiddle

Related

MYSQL full join 4 tables using a reference table

I need some help building a SQL to fetch something like a "FULL OUTER JOIN" over four tables. I have this structure and cannot really modify much on it, cause its a already in use database:
-- ----------------------------
-- Table structure for article
-- ----------------------------
CREATE TABLE `article` (
`ID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` varchar(255) NULL DEFAULT NULL,
PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3;
INSERT INTO `article` VALUES (1, 'Coffeemaker');
INSERT INTO `article` VALUES (2, 'Toaster');
-- ----------------------------
-- Table structure for language
-- ----------------------------
CREATE TABLE `language` (
`ID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` varchar(255) NULL DEFAULT NULL,
PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3;
INSERT INTO `language` VALUES (1, 'German');
INSERT INTO `language` VALUES (2, 'English');
-- ----------------------------
-- Table structure for property
-- ----------------------------
CREATE TABLE `property` (
`ID` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`Name` varchar(255) NULL DEFAULT NULL,
PRIMARY KEY (`ID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3;
INSERT INTO `property` VALUES (1, 'DescriptionText');
INSERT INTO `property` VALUES (2, 'EAN-Code');
-- ----------------------------
-- Table structure for data
-- ----------------------------
CREATE TABLE `data` (
`ArticleID` int(10) UNSIGNED NOT NULL,
`PropertyID` int(10) UNSIGNED NOT NULL,
`LanguageID` int(10) UNSIGNED NOT NULL,
`Value` varchar(255) NULL DEFAULT NULL,
PRIMARY KEY (`ArticleID`, `PropertyID`, `LanguageID`) USING BTREE,
CONSTRAINT `FK_ArticleID` FOREIGN KEY (`ArticleID`) REFERENCES `article` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_LanguageID` FOREIGN KEY (`LanguageID`) REFERENCES `language` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `FK_PropertyID` FOREIGN KEY (`PropertyID`) REFERENCES `property` (`ID`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = InnoDB;
INSERT INTO `data` VALUES (1, 1, 1, 'Eine Kaffemaschine');
INSERT INTO `data` VALUES (2, 1, 2, 'A toaster');
SQL: http://sqlfiddle.com/#!9/91dc8/1
What i want to get is a new VIEW which contains a join over all entity-tables showing a row for all articles, all properties and all languages but using the already existing data if available or null if not.
Is it possible? How would the SQL look like?
You seem to be after this...
SELECT a.id articleid
, p.id propertyid
, l.id languageid
, d.value
FROM article a
CROSS -- optional keyword
JOIN property p
CROSS -- optional keyword
JOIN language l
LEFT -- not optional
JOIN data d
ON d.articleid = a.id
AND d.propertyid = p.id
AND d.languageid = l.id;
+-----------+------------+------------+--------------------+
| articleid | propertyid | languageid | value |
+-----------+------------+------------+--------------------+
| 1 | 1 | 1 | Eine Kaffemaschine |
| 2 | 1 | 1 | NULL |
| 1 | 2 | 1 | NULL |
| 2 | 2 | 1 | NULL |
| 1 | 1 | 2 | NULL |
| 2 | 1 | 2 | A toaster |
| 1 | 2 | 2 | NULL |
| 2 | 2 | 2 | NULL |
+-----------+------------+------------+--------------------+
If you want all articles, you don't want a "full join". You want a left join that starts with the articles table. Further, you don't even need that, because all your articles have values in data.
But the question does specify all articles, so:
SELECT a.*, p.*, l.*, d.*
FROM article a LEFT JOIN
data d
ON d.ArticleID = a.ID LEFT JOIN
property p
ON d.PropertyID = p.id LEFT JOIN
language l
ON d.LanguageID = l.id;
I'm not sure what you mean by all articles and all languages (I missed the second part when I first read the question). If you want all combinations then:
SELECT a.*, p.*, l.*, d.*
FROM article a CROSS JOIN
languages l LEFT JOIN
data d
ON d.ArticleID = a.ID AND
d.LanguageID = l.ID LEFT JOIN
property p
ON d.PropertyID = p.id ;

SQL query remove only some of the duplicated rows in the results of a join

CREATE TABLE `rule` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`duplicate_mode` char(255) NOT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `rule` (`id`, `duplicate_mode`) VALUES
(1, 'single'),
(2, 'multiple');
CREATE TABLE `rule_tag` (
`rule_id` int(11) NOT NULL,
`tag_id` int(11) NOT NULL,
KEY `rule_id` (`rule_id`),
KEY `tag_id` (`tag_id`),
CONSTRAINT `rule_tag_ibfk_1` FOREIGN KEY (`rule_id`) REFERENCES `rule` (`id`),
CONSTRAINT `rule_tag_ibfk_2` FOREIGN KEY (`tag_id`) REFERENCES `tag` (`id`)
)
INSERT INTO `rule_tag` (`rule_id`, `tag_id`) VALUES
(1, 1),
(1, 2),
(2, 3),
(2, 4);
CREATE TABLE `tag` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(255) NOT NULL,
PRIMARY KEY (`id`)
)
INSERT INTO `tag` (`id`, `name`) VALUES
(1, 'foo'),
(2, 'bar'),
(3, 'bar'),
(4, 'baz');
Table visualization:
rule:
| id | duplicate_mode
| 1 | single
| 2 | multiple
rule_tag:
| rule_id | tag_id
| 1 | 1
| 1 | 2
| 2 | 3
| 2 | 4
tag:
| id | name
| 1 | Foo
| 2 | Bar
| 3 | Baz
| 4 | Bar
Wanted results (Note, the tag name that matches doesn't matter)
| rule_id | duplicate_mode | name
| 1 | single | foo
| 2 | multiple | bar
| 2 | multiple | baz
The duplicate mode column is supposed to define whether the rule is duplicated in the results
A SELECT DISTINCT with left joins would remove all duplicates, so is not a valid option.
If this is possible with a subquery, I have no idea how.
An UNION doing two separate queries, one for the single mode with a DISTINCT and another for the multiple mode without would solve it, but is there a way to do it in a single query?
I think it's also possible to do 2 joins with subqueries, but that's probably worse than UNION
If I understand correctly, you can use aggregation:
select r.id, r.duplicate_mode, max(t.name) as name
from rule r join
rule_tag rt
on rt.rule_id = r.id join
tag t
on rt.tag_id = t.id
group by r.id, r.duplicate_mode, (case when r.duplicate_mode = 'multiple' then t.id end);
Or filtering:
select r.id, r.duplicate_mode, max(t.name) as name
from rule r join
rule_tag rt
on rt.rule_id = r.id join
tag t
on rt.tag_id = t.id
where r.duplicate_mode = 'multiple' or
rt.tag_id = (select min(rt2.tag_id)
from rule_tag rt2
where rt.rule_id = rt.rule_id
);
Could be using union all between distinct and not distinct query for single and multiple
Select distinct rt.rule_id, r.duplicate_mode, t.name
from rule_tag rt
inner join rule r on r.id = rt.rule_id and duplicate_mode = 'single'
inner join tag t on t.id= rt.tag_id
UNION ALL
select rt.rule_id, r.duplicate_mode, t.name
from rule_tag rt
inner join rule r on r.id = rt.rule_id and duplicate_mode = 'multiple'
inner join tag t on t.id= rt.tag_id

MySQL ORDER BY strange sorting

Problem: mysql query return results in strange order, looks like random or so. But it happens only with one hosting, localhost and another one hosting working well. Wanna get - why it happens and how to prevent it.
Schema:
CREATE TABLE `product` (
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`sort_order` int(11) NOT NULL DEFAULT '0',
`status` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`product_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
insert into product (sort_order, status)
values
(0, 1),
(0, 1),
(0, 1),
(0, 1),
(0, 1);
CREATE TABLE `product_description` (
`product_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`product_id`),
KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
insert into product_description
values
(1, 'product_1'),
(2, 'product_2'),
(3, 'product_3'),
(4, 'product_4'),
(5, 'product_5');
CREATE TABLE `product_to_category` (
`product_id` int(11) NOT NULL,
`product_category_id` int(11) NOT NULL,
PRIMARY KEY (`product_id`,`product_category_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
insert into product_to_category
values
(1, 1),
(2, 1),
(3, 1),
(4, 1),
(5, 1);
CREATE TABLE `product_category_path` (
`product_category_id` int(11) NOT NULL,
`path_id` int(11) NOT NULL,
`level` int(11) NOT NULL,
PRIMARY KEY (`product_category_id`,`path_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
insert into product_category_path values (1, 1, 0);
Query:
SELECT p.product_id, pc.product_category_id, pd.name
FROM `product` p
LEFT JOIN `product_description` pd ON pd.product_id = p.product_id
LEFT JOIN `product_to_category` pc ON pc.product_id = p.product_id
WHERE p.status = 1 AND pc.product_category_id IN (SELECT product_category_id FROM `product_category_path` WHERE path_id = 1)
ORDER BY p.sort_order ASC;
On localhost and hosting result is always the same: 1,2,3,4,5. But on hosting it shows 1,3,2,5,4 or 2,1,5,3,4 and everytime new ordering. Why?
Update
http://dev.mysql.com/doc/refman/5.5/en/order-by-optimization.html
http://s.petrunia.net/blog/?p=24
SQL systems (of any make and model) are allowed to return result set rows in any order they find convenient unless you specify the order specifically. To put it another way, the order of a result set is formally unpredictable unless it's specified in ORDER BY. To put it a third way, on your localhost server, it's entirely accidental that your results are in the order you think they should be in. Tables have no inherent order.
You are really lucky your production server exposed this flaw in your query so quickly. Often developers don't find out about this stuff until their tables grow to tens of thousands of rows.
As this modification of your query shows (http://sqlfiddle.com/#!2/211536/2/0), all rows in your resultset have the same value of SORT_ORDER.
Query:
SELECT p.sort_order, p.product_id, pc.product_category_id, pd.name
FROM `product` p
LEFT JOIN `product_description` pd ON pd.product_id = p.product_id
LEFT JOIN `product_to_category` pc ON pc.product_id = p.product_id
WHERE p.status = 1 AND pc.product_category_id IN (SELECT product_category_id FROM `product_category_path` WHERE path_id = 1)
ORDER BY p.sort_order ASC
Results:
| SORT_ORDER | PRODUCT_ID | PRODUCT_CATEGORY_ID | NAME |
|------------|------------|---------------------|-----------|
| 0 | 1 | 1 | product_1 |
| 0 | 2 | 1 | product_2 |
| 0 | 3 | 1 | product_3 |
| 0 | 4 | 1 | product_4 |
| 0 | 5 | 1 | product_5 |
You've told SQL to order them that way. Both servers have done so.
If you need them to be ordered by PRODUCT_ID as well as SORT_ORDER just specify it (http://sqlfiddle.com/#!2/211536/4/0).
ORDER BY p.sort_order ASC, p.product_id ASC;

MySQL group_concat problems

I have multiple tables that I need to concatenate a group of results from. There is 6 tables overall with a index in each matching the primary_key in the initial table.
However, when I execute my query it results in multiple instances of each result being returned for each table. If i select just one record the output is fine. It seems that the query is being ran multiple times per table and all the results are being concatenated together.
SELECT id,
GROUP_CONCAT(tb1.table1) AS tbl1,
GROUP_CONCAT(tb2.table2) AS tbl2,
GROUP_CONCAT(tb3.table3) AS tbl3,
GROUP_CONCAT(tb4.table4) AS tbl4,
GROUP_CONCAT(tb5.table5 )AS tbl5
FROM table t
LEFT OUTER JOIN (
SELECT id, field2
FROM table1
GROUP BY id
) tb1
ON tb1.id = t.id
LEFT OUTER JOIN (
SELECT id, field2
FROM table2
GROUP BY id
) tb2
ON tb2.id = t.id
LEFT OUTER JOIN (
SELECT id, field2
FROM table3
GROUP BY id
) tb3
ON tb3.id = t.id
LEFT OUTER JOIN (
SELECT id, field2
FROM table4
GROUP BY id
) tb4
ON tb4.id = t.id
LEFT OUTER JOIN (
SELECT id, field2
FROM table5
GROUP BY id
) tb5
ON tb5.id = t.id
GROUP BY t.id
I expected/want the results to be.
Primary Key | Field 1 | Field 2 | Field 3 | Field 4 | Field 5
1 | 1, 2 | 2, 3 | 2, 4 | 1, 5 | NUll
But the returned results are, this is only an example of 1 row when not specifying a record id, however all rows appear like this.
Primary Key | Field 1 | Field 2 | Field 3 | Field 4 | Field 5
1 | 1,2 | 2,3, | 2,4 | 1,5, | NUll
2 | 1,2,1, | 2,3,2, | 2,4,2, | 1,5,1, | 1
2 | 1,2,1,2 | 2,3,2,3 | 2,4,2,4 | 1,5,1,5 | 1, 2
I am at a loss where or why the query is iterating over itself multiple times and then iterating over each table.
Any Help would be greatly appreciated.
Regards,
Edit: I have updated my initial desired results and added more rows to how the table is being brought back.
Also included a 4 table schema:
CREATE TABLE IF NOT EXISTS table (
id int(11) NOT NULL AUTO_INCREMENT,
Name varchar(255) DEFAULT NULL,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS `table2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`table1id` int(11) NOT NULL,
`Name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX (`table1id`)
);
CREATE TABLE IF NOT EXISTS `table3` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`table1id` int(11) NOT NULL,
`Name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX (`table1id`)
);
CREATE TABLE IF NOT EXISTS `table4` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`table1id` int(11) NOT NULL,
`Name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
INDEX (`table1id`)
);
INSERT INTO `table`(`id`, `Name`) VALUES (1, 'Test'),(2, 'Second Test'),(3,'Third Test'),(4, 'Test Four'),(5,'Test Five');
INSERT INTO `table2` (`id`,`table1id`, `Name`) VALUES (1,1,'Test Value'), (2,2, 'Test Value 2'), (3,3, 'Test Value 3');
INSERT INTO `table3` (`id`,`table1id`, `Name`) VALUES (1,2,'Value'), (2,3, 'Value 2'), (3,4, 'Value 3');
INSERT INTO `table4` (`id`,`table1id`, `Name`) VALUES (1,1,'Test'), (2,2, 'Test 2'), (3,5, 'Test 3');
Use DISTINCT for all columns in GROUP_CONCAT()
Try this:
SELECT t.id,
GROUP_CONCAT(DISTINCT tb1.field2) AS tbl1,
GROUP_CONCAT(DISTINCT tb2.field2) AS tbl2,
GROUP_CONCAT(DISTINCT tb3.field2) AS tbl3,
GROUP_CONCAT(DISTINCT tb4.field2) AS tbl4,
GROUP_CONCAT(DISTINCT tb5.field2) AS tbl5
FROM TABLE t
LEFT OUTER JOIN table1 tb1 ON tb1.id = t.id
LEFT OUTER JOIN table2 tb2 ON tb2.id = t.id
LEFT OUTER JOIN table3 tb3 ON tb3.id = t.id
LEFT OUTER JOIN table4 tb4 ON tb4.id = t.id
LEFT OUTER JOIN table5 tb5 ON tb5.id = t.id
GROUP BY t.id

MySQL Bug LIMIT in JOIN-Query change values in records

in the following scenario:
CREATE TABLE `table1` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`text1` varchar(29) NOT NULL,
`flag` bit(1) DEFAULT NULL,
`reference` int(11) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE ,
UNIQUE KEY `idx_text` (`text1`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `table1` (id, text1, flag, reference) VALUES
(31486, 'YWXH-D6N4-XXH6', 0, NULL),
(31487, 'CBH0-UJBC-MFTO', 0, NULL),
(31488, 'FRQM-E6MW-6VFE', 1, 1657),
(31489, 'LZOS-EYDT-1BBF', 0, NULL),
(31490, 'D1XQ-YKAX-XQRC', 0, NULL);
CREATE TABLE `table2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`value1` int(11) NOT NULL,
`value2` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `value1` (`value1`),
KEY `value2` (`value2`)
) ENGINE=MyISAM AUTO_INCREMENT=20068 DEFAULT CHARSET=utf8 ROW_FORMAT=FIXED;
INSERT INTO table2 (id, value1, value2) VALUES
(1657, 1891, 1748);
-- the tables are shortened from "real" tables, i used SHOW CREATE <table> to create this script.
are the results of the following queries different.
here mysql returns for the record with id 31488 and 31490 the wrong value for the bit-field:
Query 1:
SELECT m.id, m.text1, m.flag, m.reference
FROM table1 AS m LEFT JOIN table2 AS v ON v.id = m.reference
GROUP BY m.text1 ORDER BY m.text1 DESC LIMIT 0, 5;
returns the correct result:
id | text1 | flag | reference
31487 | CBH0-UJBC-MFTO | 0 | NULL
31490 | D1XQ-YKAX-XQRC | 0 | NULL
31488 | FRQM-E6MW-6VFE | 1 | 1657
31489 | LZOS-EYDT-1BBF | 0 | NULL
31486 | YWXH-D6N4-XXH6 | 0 | NULL
while Query 2
SELECT m.id, m.text1, m.flag, m.reference
FROM table1 AS m LEFT JOIN table2 AS v ON v.id = m.reference
GROUP BY m.text1 ORDER BY m.text1 DESC LIMIT 0, 4;
returns this:
id | text1 | flag | reference
31487 | CBH0-UJBC-MFTO | 0 | NULL
31490 | D1XQ-YKAX-XQRC | 1 | NULL
31488 | FRQM-E6MW-6VFE | 0 | 1657
31489 | LZOS-EYDT-1BBF | 0 | NULL
so here is my question:
Im using Joomla CMS, and in the code of the component i can change the whole query except the LIMIT-part.
Joomla add the limit part to the query because of the pagination.
Is there a way to change the query that it works with the LIMIT-command?
oh, my MySQL-Version on Server is 5.1.61 (but this bug still exists on my client v5.5.16)
Your a) not inserting the data correctly - see the BIT data type docs and b) not selecting the data correctly - see the docs on the Bit-Field Literals
You need to insert using the following syntax
INSERT INTO `table1` (id, text1, flag, reference) VALUES
(31486, 'YWXH-D6N4-XXH6', b'0', NULL),
(31487, 'CBH0-UJBC-MFTO', b'0', NULL),
(31488, 'FRQM-E6MW-6VFE', b'1', 1657),
(31489, 'LZOS-EYDT-1BBF', b'0', NULL),
(31490, 'D1XQ-YKAX-XQRC', b'0', NULL);
Then select like this :
SELECT m.id, m.text1, bin(m.flag), m.reference
FROM table1 AS m LEFT JOIN table2 AS v ON v.id = m.reference
GROUP BY m.text1 ORDER BY m.text1 DESC LIMIT 0, 4;
Then it all works as expected