MySQL: UNION plus JOINWhat is the difference between “INNER JOIN” and “OUTER JOIN”?What is the difference between UNION and UNION ALL?Should I use the datetime or timestamp data type in MySQL?Python join: why is it string.join(list) instead of list.join(string)?Difference between JOIN and INNER JOININNER JOIN ON vs WHERE clauseWhat's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?MySQL Limit LEFT JOIN Subquery after joiningmysql relation slowHow to optimize a select query with multiple left joins in a large database
I have accepted an internship offer. Should I inform companies I have applied to that have not gotten back to me yet?
What is the closed form of the following recursive function?
Align by center of symbol
How would someone destroy a black hole that’s at the centre of a planet?
What are the arguments for California’s nonpartisan blanket primaries other than giving Democrats more power?
What are some symbols representing peasants/oppressed persons fighting back?
Construct a pentagon avoiding compass use
Can a Reform Ger participate in Chabad programming?
Did the Shuttle's rudder or elevons operate when flown on its carrier 747?
Does optical correction give a more aesthetic look to the SBI logo?
Adding a vertical line at the right end of the horizontal line in frac
Number of optically active compounds among the products of ozonolysis
Are villager price increases due to killing them temporary?
Why is "dark" an adverb in this sentence?
Why is dry soil hydrophobic? Bad gardener paradox
Is `curl something | sudo bash -` a reasonably safe installation method?
Too many spies!
Do native speakers use ZVE or CPU?
How would you write do the dialogues of two characters talking in a chat room?
Is this more than a packing puzzle?
What's the phrasal verb for carbonated drinks exploding out of the can after being shaken?
What is 誘われて (Sasowarete) in this song lyric?
Are lithium batteries allowed in the International Space Station?
Hot object in a vacuum
MySQL: UNION plus JOIN
What is the difference between “INNER JOIN” and “OUTER JOIN”?What is the difference between UNION and UNION ALL?Should I use the datetime or timestamp data type in MySQL?Python join: why is it string.join(list) instead of list.join(string)?Difference between JOIN and INNER JOININNER JOIN ON vs WHERE clauseWhat's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?MySQL Limit LEFT JOIN Subquery after joiningmysql relation slowHow to optimize a select query with multiple left joins in a large database
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to figure out how to use UNION with the same JOIN and not to fall into #2014 - Commands out of sync.
I have created four simple tables and wrote simplified code for them. The main idea is to get each sword, gun and hat which have 'best' in name + if the owner of these is cool.
SELECT pirate_id, sword, null AS gun, null AS hat, legs = 1 AS is_cool FROM swords as t1
LEFT JOIN
(SELECT legs, pirate_id as pid FROM body_parts) AS t2
ON t1.pirate_id = t2.pid
WHERE sword LIKE '%best%'
UNION
SELECT pirate_id, null AS sword, gun, null AS hat, legs = 1 AS is_cool FROM guns as t3
LEFT JOIN
(SELECT legs, pirate_id as pid FROM body_parts) AS t4
ON t3.pirate_id = t4.pid
WHERE gun LIKE '%best%'
UNION
SELECT pirate_id, null AS sword, null AS gun, hat, legs = 1 AS is_cool FROM hats as t5
LEFT JOIN
(SELECT legs, pirate_id as pid FROM body_parts) AS t6
ON t5.pirate_id = t6.pid
WHERE hat LIKE '%best%'
ORDER BY is_cool DESC, pirate_id ASC
Currently I have to repeat JOIN each time I have to UNION. I hope there is a better way...
TABLES CREATION:
CREATE TABLE IF NOT EXISTS `body_parts` (
`pirate_id` int(11) NOT NULL,
`legs` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `body_parts` (`pirate_id`, `legs`) VALUES
(1, 0),
(2, 1),
(3, 2);
CREATE TABLE IF NOT EXISTS `guns` (
`pirate_id` int(11) NOT NULL,
`gun` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `guns` (`pirate_id`, `gun`) VALUES
(1, 'best 1');
CREATE TABLE IF NOT EXISTS `hats` (
`pirate_id` int(11) NOT NULL,
`hat` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `hats` (`pirate_id`, `hat`) VALUES
(2, 'best 2');
CREATE TABLE IF NOT EXISTS `swords` (
`pirate_id` int(11) NOT NULL,
`sword` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `swords` (`pirate_id`, `sword`) VALUES
(3, 'best 3');
mysql join union
add a comment |
I'm trying to figure out how to use UNION with the same JOIN and not to fall into #2014 - Commands out of sync.
I have created four simple tables and wrote simplified code for them. The main idea is to get each sword, gun and hat which have 'best' in name + if the owner of these is cool.
SELECT pirate_id, sword, null AS gun, null AS hat, legs = 1 AS is_cool FROM swords as t1
LEFT JOIN
(SELECT legs, pirate_id as pid FROM body_parts) AS t2
ON t1.pirate_id = t2.pid
WHERE sword LIKE '%best%'
UNION
SELECT pirate_id, null AS sword, gun, null AS hat, legs = 1 AS is_cool FROM guns as t3
LEFT JOIN
(SELECT legs, pirate_id as pid FROM body_parts) AS t4
ON t3.pirate_id = t4.pid
WHERE gun LIKE '%best%'
UNION
SELECT pirate_id, null AS sword, null AS gun, hat, legs = 1 AS is_cool FROM hats as t5
LEFT JOIN
(SELECT legs, pirate_id as pid FROM body_parts) AS t6
ON t5.pirate_id = t6.pid
WHERE hat LIKE '%best%'
ORDER BY is_cool DESC, pirate_id ASC
Currently I have to repeat JOIN each time I have to UNION. I hope there is a better way...
TABLES CREATION:
CREATE TABLE IF NOT EXISTS `body_parts` (
`pirate_id` int(11) NOT NULL,
`legs` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `body_parts` (`pirate_id`, `legs`) VALUES
(1, 0),
(2, 1),
(3, 2);
CREATE TABLE IF NOT EXISTS `guns` (
`pirate_id` int(11) NOT NULL,
`gun` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `guns` (`pirate_id`, `gun`) VALUES
(1, 'best 1');
CREATE TABLE IF NOT EXISTS `hats` (
`pirate_id` int(11) NOT NULL,
`hat` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `hats` (`pirate_id`, `hat`) VALUES
(2, 'best 2');
CREATE TABLE IF NOT EXISTS `swords` (
`pirate_id` int(11) NOT NULL,
`sword` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `swords` (`pirate_id`, `sword`) VALUES
(3, 'best 3');
mysql join union
add a comment |
I'm trying to figure out how to use UNION with the same JOIN and not to fall into #2014 - Commands out of sync.
I have created four simple tables and wrote simplified code for them. The main idea is to get each sword, gun and hat which have 'best' in name + if the owner of these is cool.
SELECT pirate_id, sword, null AS gun, null AS hat, legs = 1 AS is_cool FROM swords as t1
LEFT JOIN
(SELECT legs, pirate_id as pid FROM body_parts) AS t2
ON t1.pirate_id = t2.pid
WHERE sword LIKE '%best%'
UNION
SELECT pirate_id, null AS sword, gun, null AS hat, legs = 1 AS is_cool FROM guns as t3
LEFT JOIN
(SELECT legs, pirate_id as pid FROM body_parts) AS t4
ON t3.pirate_id = t4.pid
WHERE gun LIKE '%best%'
UNION
SELECT pirate_id, null AS sword, null AS gun, hat, legs = 1 AS is_cool FROM hats as t5
LEFT JOIN
(SELECT legs, pirate_id as pid FROM body_parts) AS t6
ON t5.pirate_id = t6.pid
WHERE hat LIKE '%best%'
ORDER BY is_cool DESC, pirate_id ASC
Currently I have to repeat JOIN each time I have to UNION. I hope there is a better way...
TABLES CREATION:
CREATE TABLE IF NOT EXISTS `body_parts` (
`pirate_id` int(11) NOT NULL,
`legs` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `body_parts` (`pirate_id`, `legs`) VALUES
(1, 0),
(2, 1),
(3, 2);
CREATE TABLE IF NOT EXISTS `guns` (
`pirate_id` int(11) NOT NULL,
`gun` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `guns` (`pirate_id`, `gun`) VALUES
(1, 'best 1');
CREATE TABLE IF NOT EXISTS `hats` (
`pirate_id` int(11) NOT NULL,
`hat` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `hats` (`pirate_id`, `hat`) VALUES
(2, 'best 2');
CREATE TABLE IF NOT EXISTS `swords` (
`pirate_id` int(11) NOT NULL,
`sword` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `swords` (`pirate_id`, `sword`) VALUES
(3, 'best 3');
mysql join union
I'm trying to figure out how to use UNION with the same JOIN and not to fall into #2014 - Commands out of sync.
I have created four simple tables and wrote simplified code for them. The main idea is to get each sword, gun and hat which have 'best' in name + if the owner of these is cool.
SELECT pirate_id, sword, null AS gun, null AS hat, legs = 1 AS is_cool FROM swords as t1
LEFT JOIN
(SELECT legs, pirate_id as pid FROM body_parts) AS t2
ON t1.pirate_id = t2.pid
WHERE sword LIKE '%best%'
UNION
SELECT pirate_id, null AS sword, gun, null AS hat, legs = 1 AS is_cool FROM guns as t3
LEFT JOIN
(SELECT legs, pirate_id as pid FROM body_parts) AS t4
ON t3.pirate_id = t4.pid
WHERE gun LIKE '%best%'
UNION
SELECT pirate_id, null AS sword, null AS gun, hat, legs = 1 AS is_cool FROM hats as t5
LEFT JOIN
(SELECT legs, pirate_id as pid FROM body_parts) AS t6
ON t5.pirate_id = t6.pid
WHERE hat LIKE '%best%'
ORDER BY is_cool DESC, pirate_id ASC
Currently I have to repeat JOIN each time I have to UNION. I hope there is a better way...
TABLES CREATION:
CREATE TABLE IF NOT EXISTS `body_parts` (
`pirate_id` int(11) NOT NULL,
`legs` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `body_parts` (`pirate_id`, `legs`) VALUES
(1, 0),
(2, 1),
(3, 2);
CREATE TABLE IF NOT EXISTS `guns` (
`pirate_id` int(11) NOT NULL,
`gun` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `guns` (`pirate_id`, `gun`) VALUES
(1, 'best 1');
CREATE TABLE IF NOT EXISTS `hats` (
`pirate_id` int(11) NOT NULL,
`hat` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `hats` (`pirate_id`, `hat`) VALUES
(2, 'best 2');
CREATE TABLE IF NOT EXISTS `swords` (
`pirate_id` int(11) NOT NULL,
`sword` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `swords` (`pirate_id`, `sword`) VALUES
(3, 'best 3');
mysql join union
mysql join union
asked Mar 26 at 6:54
Alex ShiganovAlex Shiganov
7211 bronze badges
7211 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You could use a single left join changing the using a subquery
select t1.pirate_id, t1.sword, t1.gun, t1.hat, t2.legs=1 is_cool
from (
SELECT pirate_id, sword, null AS gun, null AS hat
FROM swords
WHERE sword LIKE '%best%'
UNION
SELECT pirate_id, null, gun, null
FROM guns
WHERE gun LIKE '%best%'
UNION
SELECT pirate_id, null, null, hat
FROM hats
WHERE hat LIKE '%best%'
) t1
LEFT JOIN (
SELECT legs, pirate_id
FROM body_parts
) t2 ON t2.pirate_id = t1.pirate_id
ORDER BY is_cool DESC, pirate_id ASC
#1054 - Unknown column 'legs' in 'field list'
– Alex Shiganov
Mar 28 at 13:16
answer updated ..
– scaisEdge
Mar 28 at 17:46
#1222 - The used SELECT statements have a different number of columns
– Alex Shiganov
Mar 29 at 13:39
answer updated .. with 4 columns in each select union
– scaisEdge
Mar 29 at 16:03
You've missed one char. Anyway, thank you for the answer! Why not write 'select pirate_id, sword, gun, hat, legs = 1 AS is_cool', without table name?
– Alex Shiganov
Mar 29 at 18:46
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55351351%2fmysql-union-plus-join%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use a single left join changing the using a subquery
select t1.pirate_id, t1.sword, t1.gun, t1.hat, t2.legs=1 is_cool
from (
SELECT pirate_id, sword, null AS gun, null AS hat
FROM swords
WHERE sword LIKE '%best%'
UNION
SELECT pirate_id, null, gun, null
FROM guns
WHERE gun LIKE '%best%'
UNION
SELECT pirate_id, null, null, hat
FROM hats
WHERE hat LIKE '%best%'
) t1
LEFT JOIN (
SELECT legs, pirate_id
FROM body_parts
) t2 ON t2.pirate_id = t1.pirate_id
ORDER BY is_cool DESC, pirate_id ASC
#1054 - Unknown column 'legs' in 'field list'
– Alex Shiganov
Mar 28 at 13:16
answer updated ..
– scaisEdge
Mar 28 at 17:46
#1222 - The used SELECT statements have a different number of columns
– Alex Shiganov
Mar 29 at 13:39
answer updated .. with 4 columns in each select union
– scaisEdge
Mar 29 at 16:03
You've missed one char. Anyway, thank you for the answer! Why not write 'select pirate_id, sword, gun, hat, legs = 1 AS is_cool', without table name?
– Alex Shiganov
Mar 29 at 18:46
|
show 3 more comments
You could use a single left join changing the using a subquery
select t1.pirate_id, t1.sword, t1.gun, t1.hat, t2.legs=1 is_cool
from (
SELECT pirate_id, sword, null AS gun, null AS hat
FROM swords
WHERE sword LIKE '%best%'
UNION
SELECT pirate_id, null, gun, null
FROM guns
WHERE gun LIKE '%best%'
UNION
SELECT pirate_id, null, null, hat
FROM hats
WHERE hat LIKE '%best%'
) t1
LEFT JOIN (
SELECT legs, pirate_id
FROM body_parts
) t2 ON t2.pirate_id = t1.pirate_id
ORDER BY is_cool DESC, pirate_id ASC
#1054 - Unknown column 'legs' in 'field list'
– Alex Shiganov
Mar 28 at 13:16
answer updated ..
– scaisEdge
Mar 28 at 17:46
#1222 - The used SELECT statements have a different number of columns
– Alex Shiganov
Mar 29 at 13:39
answer updated .. with 4 columns in each select union
– scaisEdge
Mar 29 at 16:03
You've missed one char. Anyway, thank you for the answer! Why not write 'select pirate_id, sword, gun, hat, legs = 1 AS is_cool', without table name?
– Alex Shiganov
Mar 29 at 18:46
|
show 3 more comments
You could use a single left join changing the using a subquery
select t1.pirate_id, t1.sword, t1.gun, t1.hat, t2.legs=1 is_cool
from (
SELECT pirate_id, sword, null AS gun, null AS hat
FROM swords
WHERE sword LIKE '%best%'
UNION
SELECT pirate_id, null, gun, null
FROM guns
WHERE gun LIKE '%best%'
UNION
SELECT pirate_id, null, null, hat
FROM hats
WHERE hat LIKE '%best%'
) t1
LEFT JOIN (
SELECT legs, pirate_id
FROM body_parts
) t2 ON t2.pirate_id = t1.pirate_id
ORDER BY is_cool DESC, pirate_id ASC
You could use a single left join changing the using a subquery
select t1.pirate_id, t1.sword, t1.gun, t1.hat, t2.legs=1 is_cool
from (
SELECT pirate_id, sword, null AS gun, null AS hat
FROM swords
WHERE sword LIKE '%best%'
UNION
SELECT pirate_id, null, gun, null
FROM guns
WHERE gun LIKE '%best%'
UNION
SELECT pirate_id, null, null, hat
FROM hats
WHERE hat LIKE '%best%'
) t1
LEFT JOIN (
SELECT legs, pirate_id
FROM body_parts
) t2 ON t2.pirate_id = t1.pirate_id
ORDER BY is_cool DESC, pirate_id ASC
edited Mar 30 at 6:31
answered Mar 26 at 7:24
scaisEdgescaisEdge
105k10 gold badges55 silver badges73 bronze badges
105k10 gold badges55 silver badges73 bronze badges
#1054 - Unknown column 'legs' in 'field list'
– Alex Shiganov
Mar 28 at 13:16
answer updated ..
– scaisEdge
Mar 28 at 17:46
#1222 - The used SELECT statements have a different number of columns
– Alex Shiganov
Mar 29 at 13:39
answer updated .. with 4 columns in each select union
– scaisEdge
Mar 29 at 16:03
You've missed one char. Anyway, thank you for the answer! Why not write 'select pirate_id, sword, gun, hat, legs = 1 AS is_cool', without table name?
– Alex Shiganov
Mar 29 at 18:46
|
show 3 more comments
#1054 - Unknown column 'legs' in 'field list'
– Alex Shiganov
Mar 28 at 13:16
answer updated ..
– scaisEdge
Mar 28 at 17:46
#1222 - The used SELECT statements have a different number of columns
– Alex Shiganov
Mar 29 at 13:39
answer updated .. with 4 columns in each select union
– scaisEdge
Mar 29 at 16:03
You've missed one char. Anyway, thank you for the answer! Why not write 'select pirate_id, sword, gun, hat, legs = 1 AS is_cool', without table name?
– Alex Shiganov
Mar 29 at 18:46
#1054 - Unknown column 'legs' in 'field list'
– Alex Shiganov
Mar 28 at 13:16
#1054 - Unknown column 'legs' in 'field list'
– Alex Shiganov
Mar 28 at 13:16
answer updated ..
– scaisEdge
Mar 28 at 17:46
answer updated ..
– scaisEdge
Mar 28 at 17:46
#1222 - The used SELECT statements have a different number of columns
– Alex Shiganov
Mar 29 at 13:39
#1222 - The used SELECT statements have a different number of columns
– Alex Shiganov
Mar 29 at 13:39
answer updated .. with 4 columns in each select union
– scaisEdge
Mar 29 at 16:03
answer updated .. with 4 columns in each select union
– scaisEdge
Mar 29 at 16:03
You've missed one char. Anyway, thank you for the answer! Why not write 'select pirate_id, sword, gun, hat, legs = 1 AS is_cool', without table name?
– Alex Shiganov
Mar 29 at 18:46
You've missed one char. Anyway, thank you for the answer! Why not write 'select pirate_id, sword, gun, hat, legs = 1 AS is_cool', without table name?
– Alex Shiganov
Mar 29 at 18:46
|
show 3 more comments
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55351351%2fmysql-union-plus-join%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown