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 ;
Related
I want to be able to see each decks where a card is and display it as following.
A card can be in multiple decks. And there will be many cards/decks.
The server is in php.
|List of cards| In decks |
|-------------|-------------------|
|CardA |DeckA, DeckC |
|CardB |DeckC, DeckF, DeckY|
|CardC | |
The database as is:
CREATE TABLE `card` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`name` varchar(100) NOT NULL
);
CREATE TABLE `deck` (
`id` INT PRIMARY KEY AUTO_INCREMENT,
`name` varchar(100) NOT NULL
);
CREATE TABLE `deck_card` (
`deck_id` INT NOT NULL ,
`card_id` INT NOT NULL ,
PRIMARY KEY (deck_id, card_id),
FOREIGN KEY (card_id) REFERENCES card(id),
FOREIGN KEY (deck_id) REFERENCES deck(id)
);
I want to avoid having to do a request for each card.
Is there a way to request all the cards and each associated deck in one request using MySQL?
The next simple query can solve your problem:
SELECT
`c`.`name` `card_name`,
COALESCE(GROUP_CONCAT(`d`.`name`),'') `deck_names`
FROM `card` `c`
LEFT JOIN `deck_card` `dc` ON `c`.`id` = `dc`.`card_id`
LEFT JOIN `deck` `d` ON `d`.`id` = `dc`.`deck_id`
GROUP BY `c`.`name`
;
Result from SQLize.online
+===========+=============+
| card_name | deck_names |
+===========+=============+
| CardA | DeckA,DeckB |
+-----------+-------------+
| CardB | DeckC,DeckE |
+-----------+-------------+
| CardC | |
+-----------+-------------+
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
I'm having trouble performing a JOIN on tables the problem is this:
In a report cart system I have users, such as students, parents and school employees. I need to generate an SQL statement that when I enter the access ID of the parents it lists all students related to parents ID
Follow the Model:
Is this the best way to implement this "Generalization" and this relationship between parents and students, since they are all users? Can someone help me?
SQL code:
-- -----------------------------------------------------
-- Table `testeboletim`.`type_user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testeboletim`.`type_user` (
`idtype_user` INT NOT NULL AUTO_INCREMENT,
`role` VARCHAR(45) NULL,
PRIMARY KEY (`idtype_user`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testeboletim`.`user`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testeboletim`.`user` (
`iduser` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(45) NULL,
`ID` VARCHAR(20) NULL,
`birth` DATE NULL,
`telephone` VARCHAR(20) NULL,
`phone` VARCHAR(20) NULL,
`email` VARCHAR(45) NULL,
`type_user_idtype_user` INT NOT NULL,
PRIMARY KEY (`iduser`, `type_user_idtype_user`),
INDEX `fk_usuario_tipo_usuario_idx` (`type_user_idtype_user` ASC),
CONSTRAINT `fk_usuario_tipo_usuario`
FOREIGN KEY (`type_user_idtype_user`)
REFERENCES `testeboletim`.`type_user` (`idtype_user`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testeboletim`.`student`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testeboletim`.`student` (
`idstudent` INT NOT NULL AUTO_INCREMENT,
`user_iduser` INT NOT NULL,
`user_type_user_idtype_user` INT NOT NULL,
PRIMARY KEY (`idstudent`, `user_iduser`, `user_type_user_idtype_user`),
INDEX `fk_aluno_usuario1_idx` (`user_iduser` ASC, `user_type_user_idtype_user` ASC),
CONSTRAINT `fk_aluno_usuario1`
FOREIGN KEY (`user_iduser` , `user_type_user_idtype_user`)
REFERENCES `testeboletim`.`user` (`iduser` , `type_user_idtype_user`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testeboletim`.`parents`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testeboletim`.`parents` (
`idparents` INT NOT NULL AUTO_INCREMENT,
`user_iduser` INT NOT NULL,
`user_type_user_idtype_user` INT NOT NULL,
PRIMARY KEY (`idparents`, `user_iduser`, `user_type_user_idtype_user`),
INDEX `fk_responsavel_usuario1_idx` (`user_iduser` ASC, `user_type_user_idtype_user` ASC),
CONSTRAINT `fk_responsavel_usuario1`
FOREIGN KEY (`user_iduser` , `user_type_user_idtype_user`)
REFERENCES `testeboletim`.`user` (`iduser` , `type_user_idtype_user`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `testeboletim`.`student_has_parents`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `testeboletim`.`student_has_parents` (
`student_idstudent` INT NOT NULL,
`student_user_iduser` INT NOT NULL,
`parents_idparents` INT NOT NULL,
`parents_user_iduser` INT NOT NULL,
PRIMARY KEY (`student_idstudent`, `student_user_iduser`, `parents_idparents`, `parents_user_iduser`),
INDEX `fk_aluno_has_responsavel_responsavel1_idx` (`parents_idparents` ASC, `parents_user_iduser` ASC),
INDEX `fk_aluno_has_responsavel_aluno1_idx` (`student_idstudent` ASC, `student_user_iduser` ASC),
CONSTRAINT `fk_aluno_has_responsavel_aluno1`
FOREIGN KEY (`student_idstudent` , `student_user_iduser`)
REFERENCES `testeboletim`.`student` (`idstudent` , `user_iduser`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_aluno_has_responsavel_responsavel1`
FOREIGN KEY (`parents_idparents` , `parents_user_iduser`)
REFERENCES `testeboletim`.`parents` (`idparents` , `user_iduser`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
In order to solve this problem, we would need to look at testeboletim.student, testeboletim.parents, and either testeboletim.student_has_parents or testeboletim.user. I have decided to solve your problem using testeboletim.user, because it was clearer in terms of refrence keys, and what not.
Solution using testeboletim.user
Based on your question, we are looking for all rows in testeboletim.student, that have a corresponding iduser testeboletim.user based on the user_iduser of testeboletim.parents.
-- SQL Definition:
SELECT * FROM `testeboletim`.`student` WHERE `user_iduser` IN
(SELECT DISTINCT(`iduser`) FROM `testeboletim`.`user` WHERE `iduser` IN
(SELECT DISTINCT(`user_iduser`) FROM `testeboletim`.`parents`)
);
Now to do the same thing with JOIN, would require the use of LEFT JOIN; in this case testeboletim.student.
SELECT * FROM `testeboletim`.`student` AS `student`
LEFT JOIN `testeboletim`.`user` AS `user`
ON `student`.`user_iduser` = `user`.`iduser`
LEFT JOIN `testeboletim`.`parents` AS `parents`
ON `user`.`iduser` = `parents`.`user_iduser`;
Since I don't have any values, I'm going to share with you the explanation, in order to "prove" that the query works.
mysql> SELECT * FROM `testeboletim`.`student` AS `student`
-> LEFT JOIN `testeboletim`.`user` AS `user`
-> ON `student`.`user_iduser` = `user`.`iduser`
-> LEFT JOIN `testeboletim`.`parents` AS `parents`
-> ON `user`.`iduser` = `parents`.`user_iduser`;
Empty set (0.01 sec)
mysql> EXPLAIN SELECT * FROM `testeboletim`.`student` AS `student`
-> LEFT JOIN `testeboletim`.`user` AS `user`
-> ON `student`.`user_iduser` = `user`.`iduser`
-> LEFT JOIN `testeboletim`.`parents` AS `parents`
-> ON `user`.`iduser` = `parents`.`user_iduser`;
+------+-------------+---------+-------+-----------------------------+-----------------------------+---------+--------------------------+------+-------------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+---------+-------+-----------------------------+-----------------------------+---------+--------------------------+------+-------------------------------------------------+
| 1 | SIMPLE | student | index | NULL | PRIMARY | 12 | NULL | 1 | Using index |
| 1 | SIMPLE | user | ALL | PRIMARY | NULL | NULL | NULL | 1 | Using where; Using join buffer (flat, BNL join) |
| 1 | SIMPLE | parents | ref | fk_responsavel_usuario1_idx | fk_responsavel_usuario1_idx | 4 | testeboletim.user.iduser | 1 | Using where; Using index |
+------+-------------+---------+-------+-----------------------------+-----------------------------+---------+--------------------------+------+-------------------------------------------------+
3 rows in set (0.00 sec)
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
I've a vote system which is designed like this:
CREATE TABLE `vote` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`weight` int(11) NOT NULL,
`submited_date` datetime NOT NULL,
`resource_type` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2963832 DEFAULT CHARSET=latin1;
CREATE TABLE `article_preselection_vote` (
`id` int(11) NOT NULL,
`article_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `IDX_9B145DEA62922701` (`article_id`),
KEY `IDX_9B145DEAA76ED395` (`user_id`),
CONSTRAINT `article_preselection_vote_ibfk_4` FOREIGN KEY (`article_id`) REFERENCES `article` (`id`),
CONSTRAINT `article_preselection_vote_ibfk_5` FOREIGN KEY (`id`) REFERENCES `vote` (`id`) ON DELETE CASCADE,
CONSTRAINT `article_preselection_vote_ibfk_6` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
v.weight can be +1 or -1, I need, given a bunch of articles ID, to get the sum of each positive vote (+1) and the sum of negative vote (-1) per articles id.
Then my result should be
article_id | vote_up | vote_down
-----------|---------|----------
1 | 36 | 20
-----------|---------|----------
68 | 12 | 56
-----------|---------|----------
25 | 90 | 12
-----------|---------|----------
I can get that result by doing the following request, but it's quite heavy and slow on 2,000,000 votes.
SELECT apv.article_id, COALESCE(SUM(up),0) as up, COALESCE(SUM(down),0) as down
FROM article_preselection_vote apv
LEFT JOIN(
SELECT id, weight up FROM vote WHERE weight > 0 AND vote.resource_type = 'article') v1 ON apv.id = v1.id
LEFT JOIN(
SELECT id, weight down FROM vote WHERE weight < 0 AND vote.resource_type = 'article') v2 ON apv.id = v2.id
WHERE apv.article_id IN (11702,11703,11704,11632,11652,11658)
GROUP BY apv.article_id
Any ideas?
Thanks in advance.
Subselects, IN (...) and GROUP BY in one query are killers.
You should redesign to have a more traditional solution:
Have a table with the votes article_id, votes_up, votes_down, vote_date, ...
Update (cron) the summary fields in your article table votes_up, votes_down, ... with one UPDATE.
That way, you can better handle the row/table locks and have fast queries
You can try a single join:
SELECT
apv.article_id,
SUM(COALESCE(weight, 0) > 0) AS up,
SUM(COALESCE(weight, 0) < 0) AS down
FROM article_preselection_vote apv
LEFT JOIN vote
ON apv.id = vote.id
AND vote.resource_type = 'article'
WHERE apv.article_id IN (11702, 11703, 11704, 11632, 11652, 11658)
GROUP BY apv.article_id
If you need to calculate this often it might be worthwhile to denormalize your database and store a cached copy of the results.
Instead of weighting the votes, why don't you just create two tables, one for up votes and one for down votes? The only thing it will complicate is vote combination, which will still be a simple sum of the counts of two different queries.
in a nut shell do something like this:
select * from article where article_id in (1,2,3);
+------------+-----------+---------------+-----------------+
| article_id | title | up_vote_count | down_vote_count |
+------------+-----------+---------------+-----------------+
| 1 | article 1 | 2 | 3 |
| 2 | article 2 | 2 | 1 |
| 3 | article 3 | 1 | 1 |
+------------+-----------+---------------+-----------------+
3 rows in set (0.00 sec)
drop table if exists article;
create table article
(
article_id int unsigned not null auto_increment primary key,
title varchar(255) not null,
up_vote_count int unsigned not null default 0,
down_vote_count int unsigned not null default 0
)
engine = innodb;
drop table if exists article_vote;
create table article_vote
(
article_id int unsigned not null,
user_id int unsigned not null,
score tinyint not null default 0,
primary key (article_id, user_id)
)
engine=innodb;
delimiter #
create trigger article_vote_after_ins_trig after insert on article_vote
for each row
begin
if new.score < 0 then
update article set down_vote_count = down_vote_count + 1 where article_id = new.article_id;
else
update article set up_vote_count = up_vote_count + 1 where article_id = new.article_id;
end if;
end#
delimiter ;
insert into article (title) values ('article 1'),('article 2'), ('article 3');
insert into article_vote (article_id, user_id, score) values
(1,1,-1),(1,2,-1),(1,3,-1),(1,4,1),(1,5,1),
(2,1,1),(2,2,1),(2,3,-1),
(3,1,1),(3,5,-1);
select * from article where article_id in (1,2,3);