UNION SELECT after LEFT JOIN and JOIN





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}






up vote
0
down vote

favorite












    SELECT DISTINCT re.*
FROM `reports` r
LEFT JOIN `users` u
ON u.`id_rep` = r.`id`
JOIN `customers` c
ON u.`id` = r.`uid`
WHERE r.`uid` = '1' and r.`name` LIKE 'urgent'
ORDER BY r.date DESC


Without making any modifications prior to the WHERE clause, would it be possible to use UNION (or a better approach) after the LIKE statement in order to SELECT from a fourth table called workers and ORDER the results BY r.date DESC?










share|improve this question









New contributor




stackexchange is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I don't understand your EDIT. Please make a stab at the query; maybe it will be clearer then.
    – Rick James
    2 days ago

















up vote
0
down vote

favorite












    SELECT DISTINCT re.*
FROM `reports` r
LEFT JOIN `users` u
ON u.`id_rep` = r.`id`
JOIN `customers` c
ON u.`id` = r.`uid`
WHERE r.`uid` = '1' and r.`name` LIKE 'urgent'
ORDER BY r.date DESC


Without making any modifications prior to the WHERE clause, would it be possible to use UNION (or a better approach) after the LIKE statement in order to SELECT from a fourth table called workers and ORDER the results BY r.date DESC?










share|improve this question









New contributor




stackexchange is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I don't understand your EDIT. Please make a stab at the query; maybe it will be clearer then.
    – Rick James
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











    SELECT DISTINCT re.*
FROM `reports` r
LEFT JOIN `users` u
ON u.`id_rep` = r.`id`
JOIN `customers` c
ON u.`id` = r.`uid`
WHERE r.`uid` = '1' and r.`name` LIKE 'urgent'
ORDER BY r.date DESC


Without making any modifications prior to the WHERE clause, would it be possible to use UNION (or a better approach) after the LIKE statement in order to SELECT from a fourth table called workers and ORDER the results BY r.date DESC?










share|improve this question









New contributor




stackexchange is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











    SELECT DISTINCT re.*
FROM `reports` r
LEFT JOIN `users` u
ON u.`id_rep` = r.`id`
JOIN `customers` c
ON u.`id` = r.`uid`
WHERE r.`uid` = '1' and r.`name` LIKE 'urgent'
ORDER BY r.date DESC


Without making any modifications prior to the WHERE clause, would it be possible to use UNION (or a better approach) after the LIKE statement in order to SELECT from a fourth table called workers and ORDER the results BY r.date DESC?







mysql






share|improve this question









New contributor




stackexchange is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




stackexchange is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 6 hours ago





















New contributor




stackexchange is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









stackexchange

43




43




New contributor




stackexchange is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





stackexchange is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






stackexchange is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • I don't understand your EDIT. Please make a stab at the query; maybe it will be clearer then.
    – Rick James
    2 days ago


















  • I don't understand your EDIT. Please make a stab at the query; maybe it will be clearer then.
    – Rick James
    2 days ago
















I don't understand your EDIT. Please make a stab at the query; maybe it will be clearer then.
– Rick James
2 days ago




I don't understand your EDIT. Please make a stab at the query; maybe it will be clearer then.
– Rick James
2 days ago










2 Answers
2






active

oldest

votes

















up vote
1
down vote













Using Union and ORDER BY:



Be explicit with results form first query:



(SELECT DISTINCT rep.name, rep.date as rdate
FROM `reports_new` rep
LEFT JOIN `repuser_new` ru
ON ru.`id_rep` = rep.`id`
JOIN `clients` c
ON c.`id` = rep.`id_client`
WHERE ru.`id_user` = '1' and rep.`name` LIKE 'urgent')

UNION
(SELECT username,rdate FROM users)


ORDER BY rdate DESC


I assumed the second table had a date column. Otherwise and explicit value could be used.






share|improve this answer























  • There's an example of that in the manual page
    – danblack
    2 days ago






  • 1




    Try a new question. You're adding an amount of complexity beyond the initial question and without a full example with sample output clarification by comment is getting increasingly likely to be wrong.
    – danblack
    2 days ago


















up vote
1
down vote













If I understand the question correctly, yes. Each of the following is OK, though not necessarily efficient:



SELECT ...
FROM ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x

SELECT ...
FROM a
JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...

SELECT ...
FROM a
LEFT JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...

SELECT ...
FROM ( ( SELECT ... ) UNION ( SELECT ... ) ) AS a
JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...


etc. (I may have added more parentheses than necessary.)



More UNION tips:




  • Yes, the number of columns needs to be the same.

  • The default column names come from the first SELECT.

  • If you have a constant string in a column of the first SELECT, that limits the length of the matching string of the other SELECTs. (Simply pad with blanks to 'fix' the problem.)

  • If appropriate, use UNION ALL do avoid the de-duping phase, thereby being faster than UNION, which is the same as UNION DISTINCT.






share|improve this answer























  • @Rick the last 2 queries are equivalent. The ORDER BY is applied to the whole result set in both of them: dbfiddle.uk/…
    – ypercubeᵀᴹ
    2 days ago








  • 1




    @ypercubeᵀᴹ - Geez, I'm only a decade out of step -- bugs.mysql.com/bug.php?id=27848 "fixed" that in 2008 (5.0.56, 5.1.23). Thanks for rattling my cage.
    – Rick James
    2 days ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
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',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});


}
});






stackexchange is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f223394%2funion-select-after-left-join-and-join%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Using Union and ORDER BY:



Be explicit with results form first query:



(SELECT DISTINCT rep.name, rep.date as rdate
FROM `reports_new` rep
LEFT JOIN `repuser_new` ru
ON ru.`id_rep` = rep.`id`
JOIN `clients` c
ON c.`id` = rep.`id_client`
WHERE ru.`id_user` = '1' and rep.`name` LIKE 'urgent')

UNION
(SELECT username,rdate FROM users)


ORDER BY rdate DESC


I assumed the second table had a date column. Otherwise and explicit value could be used.






share|improve this answer























  • There's an example of that in the manual page
    – danblack
    2 days ago






  • 1




    Try a new question. You're adding an amount of complexity beyond the initial question and without a full example with sample output clarification by comment is getting increasingly likely to be wrong.
    – danblack
    2 days ago















up vote
1
down vote













Using Union and ORDER BY:



Be explicit with results form first query:



(SELECT DISTINCT rep.name, rep.date as rdate
FROM `reports_new` rep
LEFT JOIN `repuser_new` ru
ON ru.`id_rep` = rep.`id`
JOIN `clients` c
ON c.`id` = rep.`id_client`
WHERE ru.`id_user` = '1' and rep.`name` LIKE 'urgent')

UNION
(SELECT username,rdate FROM users)


ORDER BY rdate DESC


I assumed the second table had a date column. Otherwise and explicit value could be used.






share|improve this answer























  • There's an example of that in the manual page
    – danblack
    2 days ago






  • 1




    Try a new question. You're adding an amount of complexity beyond the initial question and without a full example with sample output clarification by comment is getting increasingly likely to be wrong.
    – danblack
    2 days ago













up vote
1
down vote










up vote
1
down vote









Using Union and ORDER BY:



Be explicit with results form first query:



(SELECT DISTINCT rep.name, rep.date as rdate
FROM `reports_new` rep
LEFT JOIN `repuser_new` ru
ON ru.`id_rep` = rep.`id`
JOIN `clients` c
ON c.`id` = rep.`id_client`
WHERE ru.`id_user` = '1' and rep.`name` LIKE 'urgent')

UNION
(SELECT username,rdate FROM users)


ORDER BY rdate DESC


I assumed the second table had a date column. Otherwise and explicit value could be used.






share|improve this answer














Using Union and ORDER BY:



Be explicit with results form first query:



(SELECT DISTINCT rep.name, rep.date as rdate
FROM `reports_new` rep
LEFT JOIN `repuser_new` ru
ON ru.`id_rep` = rep.`id`
JOIN `clients` c
ON c.`id` = rep.`id_client`
WHERE ru.`id_user` = '1' and rep.`name` LIKE 'urgent')

UNION
(SELECT username,rdate FROM users)


ORDER BY rdate DESC


I assumed the second table had a date column. Otherwise and explicit value could be used.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









danblack

1,3961212




1,3961212












  • There's an example of that in the manual page
    – danblack
    2 days ago






  • 1




    Try a new question. You're adding an amount of complexity beyond the initial question and without a full example with sample output clarification by comment is getting increasingly likely to be wrong.
    – danblack
    2 days ago


















  • There's an example of that in the manual page
    – danblack
    2 days ago






  • 1




    Try a new question. You're adding an amount of complexity beyond the initial question and without a full example with sample output clarification by comment is getting increasingly likely to be wrong.
    – danblack
    2 days ago
















There's an example of that in the manual page
– danblack
2 days ago




There's an example of that in the manual page
– danblack
2 days ago




1




1




Try a new question. You're adding an amount of complexity beyond the initial question and without a full example with sample output clarification by comment is getting increasingly likely to be wrong.
– danblack
2 days ago




Try a new question. You're adding an amount of complexity beyond the initial question and without a full example with sample output clarification by comment is getting increasingly likely to be wrong.
– danblack
2 days ago












up vote
1
down vote













If I understand the question correctly, yes. Each of the following is OK, though not necessarily efficient:



SELECT ...
FROM ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x

SELECT ...
FROM a
JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...

SELECT ...
FROM a
LEFT JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...

SELECT ...
FROM ( ( SELECT ... ) UNION ( SELECT ... ) ) AS a
JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...


etc. (I may have added more parentheses than necessary.)



More UNION tips:




  • Yes, the number of columns needs to be the same.

  • The default column names come from the first SELECT.

  • If you have a constant string in a column of the first SELECT, that limits the length of the matching string of the other SELECTs. (Simply pad with blanks to 'fix' the problem.)

  • If appropriate, use UNION ALL do avoid the de-duping phase, thereby being faster than UNION, which is the same as UNION DISTINCT.






share|improve this answer























  • @Rick the last 2 queries are equivalent. The ORDER BY is applied to the whole result set in both of them: dbfiddle.uk/…
    – ypercubeᵀᴹ
    2 days ago








  • 1




    @ypercubeᵀᴹ - Geez, I'm only a decade out of step -- bugs.mysql.com/bug.php?id=27848 "fixed" that in 2008 (5.0.56, 5.1.23). Thanks for rattling my cage.
    – Rick James
    2 days ago















up vote
1
down vote













If I understand the question correctly, yes. Each of the following is OK, though not necessarily efficient:



SELECT ...
FROM ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x

SELECT ...
FROM a
JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...

SELECT ...
FROM a
LEFT JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...

SELECT ...
FROM ( ( SELECT ... ) UNION ( SELECT ... ) ) AS a
JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...


etc. (I may have added more parentheses than necessary.)



More UNION tips:




  • Yes, the number of columns needs to be the same.

  • The default column names come from the first SELECT.

  • If you have a constant string in a column of the first SELECT, that limits the length of the matching string of the other SELECTs. (Simply pad with blanks to 'fix' the problem.)

  • If appropriate, use UNION ALL do avoid the de-duping phase, thereby being faster than UNION, which is the same as UNION DISTINCT.






share|improve this answer























  • @Rick the last 2 queries are equivalent. The ORDER BY is applied to the whole result set in both of them: dbfiddle.uk/…
    – ypercubeᵀᴹ
    2 days ago








  • 1




    @ypercubeᵀᴹ - Geez, I'm only a decade out of step -- bugs.mysql.com/bug.php?id=27848 "fixed" that in 2008 (5.0.56, 5.1.23). Thanks for rattling my cage.
    – Rick James
    2 days ago













up vote
1
down vote










up vote
1
down vote









If I understand the question correctly, yes. Each of the following is OK, though not necessarily efficient:



SELECT ...
FROM ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x

SELECT ...
FROM a
JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...

SELECT ...
FROM a
LEFT JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...

SELECT ...
FROM ( ( SELECT ... ) UNION ( SELECT ... ) ) AS a
JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...


etc. (I may have added more parentheses than necessary.)



More UNION tips:




  • Yes, the number of columns needs to be the same.

  • The default column names come from the first SELECT.

  • If you have a constant string in a column of the first SELECT, that limits the length of the matching string of the other SELECTs. (Simply pad with blanks to 'fix' the problem.)

  • If appropriate, use UNION ALL do avoid the de-duping phase, thereby being faster than UNION, which is the same as UNION DISTINCT.






share|improve this answer














If I understand the question correctly, yes. Each of the following is OK, though not necessarily efficient:



SELECT ...
FROM ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x

SELECT ...
FROM a
JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...

SELECT ...
FROM a
LEFT JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...

SELECT ...
FROM ( ( SELECT ... ) UNION ( SELECT ... ) ) AS a
JOIN ( ( SELECT ... ) UNION ( SELECT ... ) ) AS x ON ...


etc. (I may have added more parentheses than necessary.)



More UNION tips:




  • Yes, the number of columns needs to be the same.

  • The default column names come from the first SELECT.

  • If you have a constant string in a column of the first SELECT, that limits the length of the matching string of the other SELECTs. (Simply pad with blanks to 'fix' the problem.)

  • If appropriate, use UNION ALL do avoid the de-duping phase, thereby being faster than UNION, which is the same as UNION DISTINCT.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









Rick James

39.7k22256




39.7k22256












  • @Rick the last 2 queries are equivalent. The ORDER BY is applied to the whole result set in both of them: dbfiddle.uk/…
    – ypercubeᵀᴹ
    2 days ago








  • 1




    @ypercubeᵀᴹ - Geez, I'm only a decade out of step -- bugs.mysql.com/bug.php?id=27848 "fixed" that in 2008 (5.0.56, 5.1.23). Thanks for rattling my cage.
    – Rick James
    2 days ago


















  • @Rick the last 2 queries are equivalent. The ORDER BY is applied to the whole result set in both of them: dbfiddle.uk/…
    – ypercubeᵀᴹ
    2 days ago








  • 1




    @ypercubeᵀᴹ - Geez, I'm only a decade out of step -- bugs.mysql.com/bug.php?id=27848 "fixed" that in 2008 (5.0.56, 5.1.23). Thanks for rattling my cage.
    – Rick James
    2 days ago
















@Rick the last 2 queries are equivalent. The ORDER BY is applied to the whole result set in both of them: dbfiddle.uk/…
– ypercubeᵀᴹ
2 days ago






@Rick the last 2 queries are equivalent. The ORDER BY is applied to the whole result set in both of them: dbfiddle.uk/…
– ypercubeᵀᴹ
2 days ago






1




1




@ypercubeᵀᴹ - Geez, I'm only a decade out of step -- bugs.mysql.com/bug.php?id=27848 "fixed" that in 2008 (5.0.56, 5.1.23). Thanks for rattling my cage.
– Rick James
2 days ago




@ypercubeᵀᴹ - Geez, I'm only a decade out of step -- bugs.mysql.com/bug.php?id=27848 "fixed" that in 2008 (5.0.56, 5.1.23). Thanks for rattling my cage.
– Rick James
2 days ago










stackexchange is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















stackexchange is a new contributor. Be nice, and check out our Code of Conduct.













stackexchange is a new contributor. Be nice, and check out our Code of Conduct.












stackexchange is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f223394%2funion-select-after-left-join-and-join%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Ellipse (mathématiques)

Quarter-circle Tiles

Mont Emei