MYSQL: display multiple columns in 1 - mysql

I have the following query:
SELECT col1, col2, col3, col4
FROM table
WHERE ...
So the result is like:
col1 col2 col3 col4
1 2 3 4
1.1 2.1 3.1 4.1
But i want a result like:
col1,col2,col3,col4
1
1.1
2
2.1
...
any idea?
edit:
I have to search for a String in 2 tables containing 9 and 1 column(s).
Then i need to sort it all IN 1 COLUMN and only get the 5 first items.

You can do this using union clause. Somthing like this:-
SELECT col1
FROM table
WHERE ...
UNION
SELECT col2
FROM table
WHERE ...
UNION
SELECT col3
FROM table
WHERE ...
UNION
SELECT col4
FROM table
WHERE ...
Hope this helps you.

If is here no more requirements you could do like this:
select first_col
from
(
select 'col1, col2, col3, col4' as first_col, 1 as rnk
union all
select col1,2
union all
select col2,3
union all
select col3,4
union all
select col4,5
) t
order by 2
Here is an order, but probably you don't need it

Related

Using UNION ALL with the same conditions

I have 22 tables where each has many columns. I want to select 10 columns conditioning on 4 column values using WHERE. For this, I have to repeat these 4 conditions and 10 columns for all 22 tables, which is inconvenient. Is there a more efficient way to do this?
If the 22 tables have the same structure and the conditions on 4 columns are equal or similar, you can union tables in a subquery and put only one where externally.
Example:
select x.*
from
(
select col1, col2, col3, col4
from tab1
union all
select col1, col2, col3, col4
from tab2
union all
select col1, col2, col3, col4
from tab3
) x
where x.col1 = 'value'
and x.col2 = 'other value'

How can I rearrange a table to get a list of the existing combinations in Mysql?

How can I rearrange a table to get a list of the existing combinations (in both directions) in Mysql?
For example, I have a table with two columns
col1 col2
1 5
7 1
1 2
I want to get a new table (added onto the existing table) where I flip col2 and col1.
col1 col2
1 5
7 1
1 2
5 1
1 7
2 1
This allows me to see all the values for each number, when looking both directions.
Like
1: 5, 2, 7
2: 1
5: 1
7: 1
Hopefully this makes sense.
Thanks for the help!
Use union.
select col1,col2 from tbl
union
select col2,col1 from tbl
union is used instead of union all because the latter would give duplicate rows when a symmetric pair already exists in the table (for example the combination 1,5 5,1)
Then use group_concat.
select col1,group_concat(col2)
from (select col1,col2 from tbl
union
select col2,col1 from tbl) t
group by col1
If you make use off a delivered table you don't have to create a new table.
And you can make use of one query instead of using two queries.
Query
SELECT
col1,
GROUP_CONCAT(col2) AS col2
FROM
(SELECT
col1, col2
FROM
[TABLE]
UNION
SELECT
col2, col1
FROM
[TABLE]
) table_data
GROUP BY
col1
ORDER BY
col1 ASC

how to get distinct column value1 from a table which does not include a particular value in column value 2 when it has multiple rows with col val1?

Probably a big Title! sorry for that.. :(
Table 1
id col1 col2
1 10 one
2 11 two
3 10 three
Now, i would like to write a sql query to get distinct col1 from table1 which doesn't have three in col2. I need the output col1 - 11 only.
I tried like this select distinct col1 from table1 where col2 != 'three' but this gives the result as both 10 and 11. But for 10 it has corresponding row with three as col2 value. Kindly help me to find this.
Use group by and having.
select col1
from table1
group by col
having sum(case when col2='three' then 1 else 0 end)=0
If you are using MySQL, the having condition can be shortened to
select col1
from table1
group by col
having sum(col2='three')=0
as conditions are treated as booleans returning 1 for true and 0 for false.
using not in()
select distinct col1
from table1 t
where col1 not in (
select col1
from table1 i
where i.col2 = 'three'
)
or using not exists()
select distinct col1
from table1 t
where not exists (
select 1
from table1 i
where i.col1 = t.col1
and i.col2 = 'three'
)

MySQL Counting the number of occurrences of a value from a column in another column and storing in new column

How do I structure my query so I can count how many occurrences of a value in column 1 appears in column 2 and then store that result in a new column in the same table? (If a value is duplicated in the first column I still want to store the same value in the new column) For example if I had a table like this:
COL1 COL2
1 2
1 4
2 1
3 1
4 1
4 2
The resulting table will look like this:
COL1 COL2 COL3
1 2 3
1 4 3
2 1 2
3 1 0
4 1 1
4 2 1
Any help is appreciated I am new to sql! Thanks in advance!
Select
col1,
col2,
COALESCE(col3,0) as col3
FROM
mytable
LEFT JOIN
( Select count(*) as col3, col2
from mytable
GROUP BY col2) as temp ON temp.col2 = mytable.col1
And if you want the update (thanks Thorsten Kettner ) :
UPDATE mytable
LEFT JOIN ( Select count(*) as col3, col2
from mytable
GROUP BY col2) as temp ON temp.col2 = mytable.col1
SET mytable.col3 = COALESCE(temp.col3,0)
You can easily count on-the-fly. Don't store this redundantly. This would only cause problems later.
select
col1,
col2,
(
select count(*)
from mytable match
where match.col2 = mytable.col1
) as col3
from mytable;
If you think you must do it; here is the according UPDATE statement:
update mytable
set col3 =
(
select count(*)
from mytable match
where match.col2 = mytable.col1
);
To do that, you can try :
SELECT COL1, COL2, (SELECT COUNT(COL1) FROM `tablename` AS t2
WHERE t2.COL1 = t1.COL1) AS COL3 FROM `tablename` AS t1
Enjoy :)

mySQL - Selecting all records where all columns are unique

Let's say I have a table like this
id col1 col2 col3 col4 col5 col6
1 1 2 3 4 5 6
2 2 1 4 3 6 5
3 1 1 2 3 4 5
I would want to select the rows where every field has a different value. The out put would be
id col1 col2 col3 col4 col5 col6
1 1 2 3 4 5 6
2 2 1 4 3 6 5
I know you can do this
SELECT * FROM table
WHERE col1 <> col2 AND col2 <> col3...
but that would take forever with this many columns. Is there a specific function for determining if all columns are unique?
You can unpivot your table using UNION ALL:
SELECT id
FROM (
SELECT id, col1 AS col
FROM mytable
UNION ALL
SELECT id, col2
FROM mytable
UNION ALL
SELECT id, col3
FROM mytable
UNION ALL
SELECT id, col4
FROM mytable
UNION ALL
SELECT id, col5
FROM mytable
UNION ALL
SELECT id, col6
FROM mytable) AS t
GROUP BY id
HAVING COUNT(DISTINCT col) = 6
If you want all columns selected then you do something like:
SELECT *
FROM mytable
WHERE id IN ( .... above query here ...)
Unfortunately MySQL does not have an UNPIVOT operator that would make the above query less verbose.
A little less verbose than your solution, although it doesn't scale perfectly if the number of columns is excessively large:
SELECT
* -- In actual live code you would of course never use '*'
FROM
MyTable
WHERE
col1 NOT IN (col2, col3, col4, col5, col6) AND
col2 NOT IN (col3, col4, col5, col6) AND
col3 NOT IN (col4, col5, col6) AND
col4 NOT IN (col5, col6) AND
col5 NOT IN (col6) -- Done as an IN just to be consistent
Try this i hope this will helping you
select
(SELECT group_concat(DISTINCT col1) FROM table) as col1,
(SELECT group_concat(DISTINCT col2) FROM table) as col2,
(SELECT group_concat(DISTINCT col3) FROM table) as col3,
(SELECT group_concat(DISTINCT col4) FROM table) as col4