Is there a way to select these two colums:
Name 1 | Name 2
John Paul
Paul Ringo
Ringo George
So as to get both in one column with no repeated values:
Names
John
Paul
Ringo
George
SELECT name1 FROM theTable
UNION DISTINCT
SELECT name2 FROM theTable
It sounds like you can use:
SELECT Name1 FROM Table1
UNION SELECT Name2 FROM Table1
That will perform duplicate row removal by default, or you can make it explicit like this:
SELECT Name1 FROM Table1
UNION DISTINCT SELECT Name2 FROM Table1
See the docs for UNION for more information.
MySQL supports the UNION command. You just need to make sure the columns returned are the same.
E.g.
SELECT customerNumber id, contactLastname name
FROM customers
UNION
SELECT employeeNumber id,firstname name
FROM employees
Related
I'm trying to select all rows 3 times with different value attached to each one of them, for example:
MyTable:
Name
_____
Tom
John
Result:
Name MyValue
_____ _______
Tom First
Tom Second
Tom Third
John First
John Second
John Third
Anyone knows how to do it with MySQL?
You can use cross join:
select t.name, x.which
from t cross join
(select 'First' as which union all
select 'Second' as which union all
select 'Third' as which
) x
I have two tables, 1 look like
tableA
id name value
------------------
1 Joe 22
2 John 50
3 Joe 38
4 Joe 10
5 John 20
I need to add all the value of value with same name from tableA and store in the tableB like this
tableB
id name value
---------------
1 Joe 70
2 John 70
PLEASE HELP
Assuming the tableb already exists, you can insert the data like this:
insert into tableb(id, name, value)
select min(id), name, sum(value)
from tablea
group by name;
Hi use (Select * INTO ) like this.
SELECT * INTO Table2
FROM Table1
insert into tableb(id, name, value)
select id, name, sum(value)
from tablea
group by name,id;
Kindly answer if possible.
Query to select record of table repeatedly two or more times to each row.
Like:
ID1 Name1 Age1
ID1 Name1 Age1
ID1 Name1 Age1
ID2 Name2 Age2
ID2 Name2 Age2
ID2 Name2 Age2
.. .. An so on.
Combination of UNION ALL and ORDER BY will do.
(SELECT * FROM TABLE_NAME) UNION ALL
(SELECT * FROM TABLE_NAME) UNION ALL
(SELECT * FROM TABLE_NAME)
ORDER BY 1,2,3
The above example is for 3 repeated times. If you want more, just add more UNION statements.
Is it possible to join two tables in mysql into one single table , both tables have the same column names ...?
table a has
id name state
1 jose up
2 sam mp
3 jack tn
table b is
id name state
4 ken ker
5 sk wb
is it possible to join both like:
id name state
1 jose up
2 sam mp
3 jack tn
4 ken ker
5 sk wb
Use UNION ALL
SELECT
id,name,state
from
tbla
UNION ALL
SELECT
id,name,state
from
tblb
If you want the exact duplicates to be excluded from the output. Use UNION
SELECT
id,name,state
from
tbla
UNION
SELECT
id,name,state
from
tblb
Reference:
13.2.8.4 UNION Syntax
The requirement you are describing is a union all query, not a join:
SELECT id, name, state
FROM table_a
UNION ALL
SELECT id, name, state
FROM table_b
I have a query that returns data in the following format:
id | name | number
1 John 12545
1 John 50496
2 Mary 23443
3 Mark 54
3 Mark 5600
3 Mark 50206
I would like to find out the number of distinct ids that appear in the result set. For example, for the result above. I would like to obtain the value 3.
Is there any way to add a column so the result looks like this instead?
count | id | name | number
3 1 John 12545
3 1 John 50496
3 2 Mary 23443
3 3 Mark 54
3 3 Mark 5600
3 3 Mark 50206
My query is:
SELECT * FROM (
SELECT id FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) totalCount,
id,name,number
FROM tableName
or by using CROSS JOIN
SELECT x.totalCount,
a.id, a.name, a.number
FROM tableName a, (SELECT COUNT(DISTINCT id) totalCount
FROM tableName) x
You should try :
SELECT id,name,number, (SELECT COUNT(DISTINCT name) FROM YourTableName) FROM YourTableName
Good luck
SELECT COUNT(DISTINCT id) would be faster than using column name.
SELECT (SELECT COUNT(DISTINCT id) FROM tableName) as 'count',
id,name,number
FROM tableName
SELECT COUNT(id) AS count , id, name, number
FROM
(
SELECT id
FROM tableA
WHERE xyz
) as t1
JOIN tableB using (id)
GROUP BY id, name, number