I want column names of my sql table - mysql

My table looks like:
id column1 column2 column3 // feilds names
1 1 0 1 // row
Here I need the columns names of table which contain 1 with respect to "id".
Output I required:
column1 column3
(which has data 1 with respect to id=1).

You haven't mentioned what DBMS you are using; that would be useful. With PostgreSQL you achieve that by using the UNION construct, which is used to combine the result set of multiple SELECT statements:
SELECT id,column1 FROM "Table1" where id=column1
UNION
SELECT id,column2 FROM "Table1" where id=column2
UNION
SELECT id,column3 FROM "Table1" where id=column3;
However, this removes duplicates from the final result set. To also retrieve duplicates use the UNION ALL constrct:
SELECT id,column1 FROM "Table1" where id=column1
UNION ALL
SELECT id,column2 FROM "Table1" where id=column2
UNION ALL
SELECT id,column3 FROM "Table1" where id=column3;
One disadvantage with this approach is that you need as many select statements as there are columns. A second disadvantage is that your final result set will have the column labels of the first select statement (i.e. id, column1 in this example).

Related

UNION not removing duplicates

I am trying to query 2 tables, return the values while combining duplicates. While UNION is supposed to remove duplicates, it seems to fail in this case. Note that this query is sent with PHP.
QUERY
<?php
$keywords = 'MOTOR';
$toSend = "SELECT part as Column1, '' as Column2 FROM soldParts WHERE part LIKE '%".$keywords."%' UNION SELECT part as Column1, vin as Column2 FROM vinData WHERE part LIKE '%".$keywords."%' ORDER BY column1 ASC";
?>
CURRENT OUTPUT
Column1 Column2
motor 1
motor 2
motor 2
CCDD44
motor 3
AABB1122
motor 3
DESIRED OUTPUT
Column1 Column2
motor 1
motor 2
CCDD44
motor 3
AABB1122
I have looked up several similar questions. However, my second column should just be merged (or concatenated), unlike other questions and/or they do not involve multiple tables. Also note that table soldParts Column2 has no value.
Your resultset actually has no duplicates. Duplicates are rows where all columns have equal values, and no row in your resultset complies to that definition.
Presumably, you want aggregation in the outer query:
select column1, max(column2) column2
from (
select part as column1, null as column2 from soldparts where part like ?
union all
select part as column1, vin as column2 from vindata where part like ?
)
group by column1
order by column1 asc
Note that I modified your query to use bind parameters (?); for the sake of security and efficiency, you should learn to use parameterized query rather than concatenating variables in the query string.
To get the result you want you'll need to perform an aggregation on top of the UNION (but before the ORDER BY).
For example:
select Column1, max(Column2) as Column2
from (
SELECT part as Column1, '' as Column2
FROM soldParts
WHERE part LIKE ?
UNION
SELECT part, vin
FROM vinData
WHERE part LIKE ?
) x
GROUP BY Column1
ORDER BY Column1

SQL: Alternative to COALESCE() function that groups all values into one column

I have a table like this
Using the COALESCE() function...
SELECT COALESCE(column1, column2, column3) combinedColumn from t;
I get this...
However, I want this....
I found a work around using UNION ALL but this isn't very elegant. Is there a function that works like COALESCE() except includes all values? Thanks
You can't use a coalesce here as there might be more than one value to return
UNION is the best solution (not UNION ALL because blanks)
select column1 from mytable
UNION
select column2 from mytable
UNION
select column3 from mytable
That said, if you want to maintain duplicates (if any), it's UNION ALL, or joining all the columns into a single string then splitting them out again (avoiding the UNION ALL at any cost)

Generate MQsql query to get records

I am having the following table say "A"
"column1" "column2"
1 arafath#gmail.com
2 ram#gmail.com;arafath#gmail.com
3 tom#gmail.com
I want to get the records with the following condition.
Condition1:
If the column value exist in the any of the row, it will retrieve the matched rows
Condition2:
If the column value doesn't match with any of the row, it wants to retrieve all the rows
Eg: column2 = "ram#gmail.com"
Output should be "row 2"
Eg: column2 = "arafath#gmail.com"
Output should be "row 1, row 2"
Eg: column2 = "xxx#gmail.com" (Unmatched column)
Output should be all the rows (row 1, row 2, row 3)
Please help me out to solve the problem.
Thanks in advance.
Please try the below one.
SELECT col1, col2
FROM yourTable
where ( not exists (Select col2
FROM yourTable where col2 like 'xxx#gmail.com')
or col2 like 'xxx#gmail.com');
We can try using a union here:
SELECT col1, col2
FROM yourTable
WHERE col2 REGEXP '[[:<:]]ram#gmail.com[[:>:]]'
UNION ALL
SELECT col1, col2
FROM yourTable
WHERE col2 NOT REGEXP '[[:<:]]ram#gmail.com[[:>:]]' AND
NOT EXISTS (SELECT 1 FROM yourTable WHERE col2 REGEXP '[[:<:]]ram#gmail.com[[:>:]]');
Demo
The above strategy is that the first half of the union returns the matching record, if it exists. The second half of the union then returns all other records, but only if on match were found in the first half of the union. If a match were found, then the WHERE clause in the second half of the union would fail, and would return nothing.
Also, please note that storing comma separated (or semicolon separated) data in your MySQL tables is generally bad practice. I had to use REGEXP to get around this problem, but ideally if each email had a separate row, we would only need to use = equality.

MySQL take from One Row and Create Two Rows

This might sound a little unorthodox but I haven't been able to find out if this even works. I have a MySQL query that fills columns into a single row.
Select
projectnumber,
highnumber,
lownumber
From
Table1
WHERE
project = 'THIS'
and sequence = '0'
This would return on 1 row with 3 columns "projectnumber", "highnumber", "lownumber". I am needing to return 2 rows, both would have the same "projectnumber" but one row would have "highnumber", and the second row would have the "lownumber" with an "A" appended to the end of it.
Is this even plausible?
Use UNION ALL to "mix" two statements, and use CONCAT() to append "A" to a cloumn:
SELECT projectnumber, highnumber FROM Table1 WHERE ...
UNION ALL SELECT projectnumber,CONCAT(lownumber,"A") FROM Table1 WHERE ...

When querying multiple tables using UNION ALL the AS keyword only works for the first table

I have a query:
(SELECT col1 AS table1 FROM table1 WHERE col3 IS NOT NULL)
UNION ALL
(SELECT col1 AS table2 FROM table2 WHERE col3 IS NOT NULL)
UNION ALL
(SELECT col1 AS table3 FROM table3 WHERE col3 IS NOT NULL)
However when I process this using PDO and the fetchAll(PDO::FETCH_ASSOC); command, the keys to the array generated all come out as table1 irrespective of the table they are actually from.
Is my syntax incorrect? Thanks!
Your query returns a single column. A single column can only have one name/alias. In a UNION query, the first subquery defines the resulting set's column names.
If you want to specify which table each value has come from, add another column, e.g. like this:
(SELECT col1, 'table1' AS src FROM table1 WHERE col3 IS NOT NULL)
UNION ALL
(SELECT col1, 'table2' FROM table2 WHERE col3 IS NOT NULL)
UNION ALL
(SELECT col1, 'table3' FROM table3 WHERE col3 IS NOT NULL)
That's the SQL specification: The column names of the result set are taken from the first select.
It's just the way it is.
When you think about it, having different column names in subsequent selects makes no sense, because a column name can't change part way through a result set - column names are defined (once) for the (entire) result set.
Yes, this the way it works, the unioned values will have the alias from the column in the first query. Quoted from UNION documentation page:
The column names from the first SELECT statement are used as the
column names for the results returned.
If you didn't need that, give that column the alias you want.