Is it possible to:
SELECT * FROM table1 , table2 ORDER BY (a UNION)
I tried that but doesn't work.
I looked on Google for some answers but got nothing and I don't know how to look anymore, what to search so this is my last solution: ask here. Maybe one of you knows a clause I don't and would help in my case. I don't know how else to think this query...
The union is made between two columns from two tables (or more). So i want to order every possible row by this new column made with union. Something like (so this will be generic) :
SELECT * FROM table1 , table2 ORDER BY ((SELECT col1 AS col FROM table1) UNION ALL (SELECT col2 AS col FROM table2) ORDER BY col DESC);
Try this query like that :-
SELECT * FROM(
SELECT * FROM table1
UNION
SELECT * FROM table2
) as tab ORDER BY col_name
If you want to do the union and then order, you can do:
select t1.*
from table1 t1
union
select t2.*
from table2 t2
order by a;
Notes:
Use union all rather than union, unless you specifically want to incur the overhead of removing duplicates.
The use of * implies that the two tables have the same columns in the same order (and compatible types).
Related
I am looking to run this query on a list of tables.
SELECT Description,Code,count(*) as count
FROM table1
group by Description,code
having count(*) > 1
I will have to run this query on 30+ different tables, I was wondering If I could change the from statement and just list off the table names.
In addition, is there some functionality that will add the name of the table that it came from in a seperate column to distinguish where the results came from?
Thanks in advance
You might use UNION ALL to put it together. Unless you need some dynamic table selection.
SELECT Description,Code,count(*) as count, 'table1' as tableNane
FROM table1
group by Description,code
having count(*) > 1
UNION ALL
SELECT Description,Code,count(*) as count, 'table2' as tableNane
FROM table2
group by Description,code
having count(*) > 1
...
Actualy I like #Shubhradeep Majumdar version. It will generate more concise code.
SELECT Description,Code, Count(Code), tableName FROM (
SELECT Description,Code, 'table1' as tableName
FROM table1
UNION ALL
SELECT Description,Code, 'table2' as tableName
FROM table2
) tables
GROUP BY tableName, Description, Code
HAVING COUNT(Code) > 1
But there might be a little catch to it. It is more elegant code, but it might actually be slower than first version. The problem is that tableName is appended at every record before grouping while in my first version you do that on already processed data.
Carrying over from #Marek's answer, You could first append all the tables to a table with union all.
select *, 'tab1' as tabnm from tab1
union all
select *, 'tab2' as tabnm from tab2
union all
select *, 'tab3' as tabnm from tab3
-- and so on...
And then use your code to process that final table.
will save you a great deal of time.
EDITED with a column specifying the table name
I am trying to get my head around using GROUP_CONCAT within MYSQL.
Basically I have the following table, table1:
id, field1, field2, active
I want to bring back 5 rows within the table but in random order. So I'm using this:
SELECT GROUP_CONCAT(id ORDER BY rand()) FROM table1 WHERE active=1
This behaves as I would expect. I then want to use the output to select the other columns (field1, field2) from the table and display the results.
So I've tried using:
SELECT *
FROM table1
WHERE id IN
(
SELECT GROUP_CONCAT(id ORDER BY rand()) as id FROM table1 WHERE active=1
);
I expected something like the above to work but I cant figure out why it doesn't. It DOES bring back results but not all of them, (i.e.) my table contains 10 rows. 6 rows are set to active=1. Therefore I would expect 6 rows to be returned ... this isn't happening I may get 1,2 or 0.
Additionally if it helps I'd like to limit the number of results returned by the sub-query to 3 but adding LIMIT doesn't seem to have any affect on the results returned.
Thank you in advance for your help
I think this is what you are looking for. This will bring back 5 random active rows.
SELECT *
FROM table1
WHERE active=1
ORDER BY RAND()
LIMIT 5;
why not use this :
SELECT *, GROUP_CONCAT(id ORDER BY rand()) as randoms FROM table1 WHERE active=1
If I understand correctly, you are trying to build a query like this:
select *
from table1
where id in (1,2,3,4,5) -- Just an example
and you are trying to "fill" the in condition with a group_concat() result.
That's not the way to do it.
You only need to specify the subquery in the parenthesis:
select *
from table1
where id in (select id from table1 where active=1)
Notice some additional things:
The order by rand() is irrelevant, because the in () will be evaluated regardless of the order of the values.
In this particular scenario, I would recommend to use a join instead of in.
Using join:
select t1.*
from
table1 as t1
inner join table1 as t2 on t1.id = t2.id
where t2.active=1
i have have two identical tables
lets call them t1 and t2
and i want to show data from both of them BUT they may have identical rows (lets say these rows have the same id )
in that case i only want to get the row from table 1 and ignore the one from second table .
so
SELECT column_name(s) FROM t1 UNION SELECT column_name(s) FROM t2
but how should i handle duplicates ?
you mean this ?
select column1 , column2 from (
SELECT column1 , column2 FROM t1
UNION
SELECT column1 , column2 FROM t2
)t
group by column1
you will have distinct column1 here
If you want to remove duplicates from a SELECT result set, you could use the DISTINCT clause with a sub-query
SELECT DISTINCT * FROM (SELECT value FROM t1 UNION SELECT value FROM t2) AS S
Or better, you might use the UNION DISTINCT syntax:
SELECT value FROM t1 UNION DISTINCT SELECT value FROM t2;
BTW, for UNION the default is UNION DISTINCT (whereas for SELECT, SELECT ALL is the default), so this could be rewritten as:
-- without specifier UNION is implicitly DISTINCT
SELECT value FROM t1 UNION SELECT value FROM t2;
... which is in fact the query you proposed. What was wrong with that? It works with my test set: http://sqlfiddle.com/#!2/d4812/1
Maybe a sqlfeedle with your actual table content might help to provide a better answer.
Here is one way:
SELECT column_name(s)
FROM t1
UNION
SELECT column_name(s)
FROM t2
where not exists (select 1
from t1
where t1.col1 = t2.col1 and t1.col2 = t2.col2 and . . .
)
I've been left with some code that looks like this:
SELECT DISTINCT ee.* FROM exp_extensions ee WHERE enabled = 'y'
Our db admin is screaming about the select all, and wants us to grab all the individual fields separately. I've never seen a SELECT DISTINCT * before; how would I rewrite that?
Option 1:
SELECT DISTINCT ee.extension_id, ee.class, ee.method, ee.hook, ee.settings, ee.priority, ee.version, ee.enabled FROM exp_extensions ee WHERE enabled = 'y'
Option 2:
SELECT DISTINCT (ee.extension_id, ee.class, ee.method, ee.hook, ee.settings, ee.priority, ee.version, ee.enabled) FROM exp_extensions ee WHERE enabled = 'y'
Or some other way entirely?
There is no difference between Option 1 or Option 2. Say you have a table that has one column col1 that has one row. All of these produce the same result:
SELECT DISTINCT * FROM t1
SELECT DISTINCT col1 FROM t1
SELECT DISTINCT (col1) FROM t1
SELECT * FROM t1
SELECT col1 FROM t1
SELECT (col1) FROM t1
All the * does is essentially expand to all available columns on the table.
I'm trying to achieve is to create one complex query consisting of a few sub-queries. The idea is to give it to a business person to run on a weekly basis to pull reporting data.
The effect would be similar to the query below, where all data from many tables are displayed in one result.
select * from table1, table2, table3
So I need something like, but it's not working.
select
(select * from table1 where ...... ) as table1,
(select * from table2 where....... ) as table2
Manually, I could run the sub-queries separately, then manually append the results into one big excel sheet. But I want to make it easier for the business person to do this, and minimize errors.
Is this possible in MySQL?
The reason for this is I'm converting a legacy Oracle PIVOT SQL statements into the MySQL equivalence, and the sub-queries are pretty complex.
I can provide the Oracle SQL if needed.
Much appreciated as always.
After some fiddling around:
select * from
(select * from table1 where survey_user_id=4 ) as T1
,
(select * from table2 where survey_field_type_id=100 ) as T2
,
(select * from table3 ) as T3
If i understand you correctly you just need UNION :D
(SELECT column1 AS name1, column2 AS name2 FROM table1 WHERE ...... )
UNION
(SELECT column3 AS name1, column4 AS name2 FROM table2 WHERE ...... )
UNION
....
As mentioned bellow in comment,
columns need to have the same name (you can use aliases for it) and stay in the same order.
select main.*,
(select col from tbl1 where tbl1.id=main.id) as col1,
(select col from tbl2 where tbl2.id=main.id) as col2,
(select col from tbl3 where tbl3.id=main.id) as col3
from master as main