MYSQL Echo Value only once from multible columns - mysql

I would like print out a list of names, each name only once.
The Problem: I have two different columns with names in them, yet I want to display each name only once, not depending on the column.
I am going to echo the result with PHP, so also loop's or other PHP actions are possible if there is no MYSQL only solution.
To visualise an example: (I also have an ID column as shown below)
ID |Name1 | Name2
-------|-------|-------
01 | A | B
02 | A | C
03 | B | A
As seen above there are the same names in both columns, yet I want to get each name only once.
Locking like this (for example):
(List:)
A
B
C

something like this should do it:
select distinct name
from (
select Name1 name from table_name
union all
select Name2 name from table_name
)

try this:
select distinct(name) from (select a.name1 as name from tablename as a
union select b.name2 as name from tablename as b)name;

Related

How to split column into multiple rows (with pipe as separator) in mysql?

I've table1 like this:
col_raw
| COL1 |COL2| COL3 | COL4 |
-------------------------------------
|0123456789|Male|Basketball|Aquarius|
-------------------------------------
I would like to convert table1 to table2 which is consist of this column header name
table2:
Column Names: Phone Number | Gender | Hoby | Zodiak
Let me know if you ever query like this in mysql. Thank you!
In case, you always have 4 columns, you can use the below
select substring_index(column_name,'|',1) firstcol,
substring_index(substring_index(column_name,'|',2),'|',-1) secondcol,
substring_index(substring_index(column_name,'|',3),'|',-1) thirdcol,
substring_index(substring_index(column_name,'|',4),'|',-1) fourthcol
from table_name;
To understand the above, you can try running the below query, you will see how the substring_index is being used multiple times in the above query to fetch the required details
select substring_index(column_name,'|',1),
substring_index(column_name,'|',2),
substring_index(column_name,'|',3),
substring_index(column_name,'|',4)
from table_name;

SQL select data from multiple tables

I'm trying to select from multiples tables(10+) that are in the following format using mySQL.
+----------table(n)-----------+
+-----------------------------+
| url | id | ... |
+-----------+-----------+-----+
| url1.com | 12345678 | ... |
| url2.com | 45832458 | ... |
What I need to do is retrieve the id from each table for a given URL and return it in a single row like so.
+--------------table(n)--------------+
+------------------------------------+
| url | table(n) | table(n+1) |
+-----------+-----------+------------+
| url1.com | 12345678 | 8182735 |
But the URL might not exist for a given table, so I just need to retrieve all the ids from the tables where the URL is found.
For example, I have 10 tables and a URL/id is in 4 of them. I need to retrieve these 4, in a single row. I've tried to use aliases for columns along with various JOIN combinations to no avail.
Any help would be appreciated.
You can do this with a union all and group by:
select url,
max(id1) as id1, max(id2) as id2, . . .
from ((select url, id as id1, NULL as id2, . . . from table1) union all
(select url, NULL as id1, id as id2, . . . from table1) union all
. . .
) t
group by url;
try like this
SELECT tu.URL,COALESCE(table_1.id,0) AS table_1,COALESCE(table_2.id,0) As
table_2,......,COALESCE(table_n.id,0) AS table_n FROM Table_URL tu
LEFT JOIN table_1 ON tu.URL = table_1.URL
LEFT JOIN table_2 ON tu.URL = table_2.URL
.
.
LEFT JOIN table_n ON tu.URL = table_n.URL
You want a variable number of columns generated by your SQL statement, and you cannot do that with MySQL.
You can see some techniques to get around it here: MySQL pivot row into dynamic number of columns
Many reporting tools will let you do things like you want to, but using some sort of prepared statement hack with MySQL would be the only way to do it:
http://buysql.com/mysql/14-how-to-automate-pivot-tables.html

MySQL select column name and value as a field

I have a mysql table that looks something like this:
id | PO | DAP | MEDIA
---|----|-------|------
1 | 2 | 34 | 64
2 | 6 | 53 | 23
I would like to be able to query get multiple rows, one for each column. E.g:
SELECT column_name as column, column_value as value FROM my_table;
Which would give me:
PO=2,DAP=34,MEDIA=54,PO=6,DAP=53,MEDIA=23
What would I need to use to formulate a query like this?
You have to first CONCAT the data of each specified field and apply GROUP_CONCAT ON the result.
Query
SELECT GROUP_CONCAT(temp_col) FROM
(
SELECT 1 as 'temp_id',
CONCAT(
CONCAT('PO=', PO),
',',
CONCAT('DAP=', DAP),
',',
CONCAT('MEDIA=', MEDIA)
) AS 'temp_col'
FROM test
) temp
GROUP BY temp_id
Check Out SQLFIDDLE
Not exactly sure what you mean. But this is traditionally done in this manner
SELECT * FROM my_table;
You'll get your array like this
array(0=>array('PO'=>2,'DAP'=>34,'MEDIA'=54), 1=>array('PO'=>6, 'DAP'=>53, 'MEDIA'=> 23))
.. like so.

MySQL: Retrieving all fields SELECT * when one of the fields is binary and needs to HEX

I have a primary kew which is a binary(16) UUID in a field called binid.
I would like to get ALL columns including the binary id with a SELECT statement.
I know I can do "SELECT * FROM TABLE", but how to combine with HEX(binid)?
This works when I get individual fields: "SELECT HEX(binid) AS binid FROM TABLE"
but I dont want to mention all of the fields (too many). Is there a way to get ALL and HEX in one statement?
PS. I am creating based on stackoverflow question: How to store uuid as number?
Selecting * will select all the fields as they are, without transformations. To select them changing their formats you will have to specify all the fields you want to select spearated by ,.
For instance:
SELECT HEX(`binid`) as `bindid`, `name`, FROM_UNIXTIME(`birthday`) as `birthday`, `gender` FROM `table`;
You can apply as many transformations you want.
You can also do this:
SELECT HEX(`binid`) as `hexdid`, * FROM `table`;
In this case the result will have both the binid in hex format, named hexid, and also the original binid aswell with the other fields of the table.
select col1, col2, col3 from table
e.g. if table is called T and set as thus:
T
=================
| binid | A | B |
| 0 | 1 | a |
| 1 | 2 | b |
| 10 | 3 | c |
select hex(binid), A, B from table
You could do:
Select *, HEX(binid) as clearBinId from Table
This will select all the columns (including binid) and also select the HEX(binid) as a column called 'clearBinId'

MySQL : interval around id column and return another one from subquery with multiple columns

I would like to run a query from a table where the content is like that :
id | col1 | col2 | col3
-----------------------
1 | i_11 | i_12 | i_13
2 | i_21 | i_22 | i_23
3 | i_31 | i_32 | i_33
.. | ... | ... | ...
SELECT col1 FROM table WHERE id IN
(SELECT id-1, id+1 FROM table WHERE col1='xxx' AND col2='yyy' AND col3='zzz')
The aim is to get an interval [id-1, id+1] based on the id column which returns the content stored in col1 for id-1 and id+1. The subquery works but I guess I have a problem with the query itself, since I'm having an error "Operand should contain only one column". I understand it, but I don't see any other way to do it in one query ?
I'm quite sure there's a pretty easy solution but I can't figure it out for the moment, even after having carefully read other posts about multiples columns' subqueries...
Thank you for any help :-)
The only way I can think to do it right now is like this:
SELECT col1
FROM table T
WHERE id BETWEEN (SELECT id FROM table WHERE col1='xxx' AND col2='yyy' AND col3='zzz') -1
and (SELECT id FROM table WHERE col1='xxx' AND col2='yyy' AND col3='zzz') +1
Your problem is that you are retrieving two values - but as a list rather than a set. The SQL optimizer can't see 1,3 as a set of two items when they are presented in a single row. There may also be a cast needed.
This should work.
SELECT col1 FROM table WHERE id in
(
select cast(id as int) -1 from table where col1='i_21'
union
select cast(id as int) +1 from table where col1='i_21'
)