I'm making this select:
select code,name from talbe1
union all
select code,name from table2
The actual code isnt important to me but what is important to me is that the code column will be unique column, and with this select I cant grantee it..
Is there any save word/something that will give me something like that:
select getUniqueCode(),name from(
select name from talbe1
union all
select name from table2)
Thanks.
Have a look at the mysql UUID call. Which would result in something like this:
select UUID(),name from(
select name from talbe1
union all
select name from table2)
remove "all":
select code,name from table1
union
select code,name from table2
Union all keeps al rows.
Union removes duplicates.
If you have different names for the same code in each table, you have to pick one name - try this:
select code, max(name) as name
from (select code,name from table1
union
select code,name from table2) x
group by 1
Related
[Expected Result]
I Need this result from Single table dont know how to do this.
You can use union all to duplicate each and every row in the resultset:
select custname, city from mytable
union all select custname, city from mytable
An order by clause might be useful:
select custname, city from mytable
union all select custname, city from mytable
order by custname, city
One simple method is:
select t.*
from t
union all
select t.*
from t;
If you care about the ordering, then you need an order by clause, so add:
order by custname;
Add ORDER BY to the UNION. For example:
select * from t union all select * from t order by id
See running example at DB Fiddle.
Excuse my ignorance. I want to take union of the tables invcount2 and reserve1. What is the error in the below code?
mysql_query("select distinct Name from exam1sem.invcount2 WHERE Name not in
(select Name from exam1sem.invcount2 where Date='$date' AND Time='$time'
union select Name from exam1sem.reserve1 where Date='$date' AND
Time='$time')
union
select distinct Name from exam1sem.reserve1 WHERE Name not in (select Name
from exam1sem.invcount2 where Date='$date' AND Time='$time'
union select Name from exam1sem.reserve1 where Date='$date' AND
Time='$time'
)
order by Avail, TD, NOD
");
Well it seems, You cannot order by something which is not present in select statement when you are using distinct keyword.
You can check this SQL link
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
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).
How can I select which table each result is from, when I use a UNION command to search multiple tables?
For example, if there are results from both tables, how can I add a column that will say (or differentiate between) whether it is from tableA or tableB.
try this one, simply add a virtual column for the name of the table.
SELECT *
FROM
(
SELECT *, 'tableA' as tableName FROM tableA
UNION ALL
SELECT *, 'tableB' as tableName FROM tableB
UNION ALL
SELECT *, 'tableC' as tableName FROM tableC
) s
WHERE colName = 'hello'