UNION not removing duplicates - mysql

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

Related

Use temporary table in query

Table contains multiple columns and multiple redundant rows which I don't need to work on. Let's say I selected columns and rows which I need to work on.
select column1,
column2,
column3
from table
where column1 > something
and column2 == something;
Now how do I perform nested query on that selected data? I was thinking of doing something like.
select column1,
sum(column2) from (
select column1,
column2,
column3
from table
where column1>something
and column2 == something)
group by column1;
And I am getting error. Any help would be appreciated
I don't know if you really need the subquery or the column3 that you don't use in any condition , if you provide template datas and expected result would be better.
For your query to work you need an alias in the subquery, so it would be something like:
select t1.column1,
sum(t1.column2) from (
select column1,
column2,
column3
from table
where column1>something
and column2 == something) as t1
group by t1.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)

I want column names of my sql table

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).

Want to choose something between count(*) or count(distinct *)

Let's say I have a table
with
Column1
A
A
A
B
B
C
C
C
Column2
CH
FH
FH
BW
CH
AW
Now: I want to have a select sth---> result 6 different combinations of column1 and column 2.
If i say select count(column2): I'll take 8.I don't want it.
If i say select(distinct column2): I'll take 5.I don't want it either.
The 6 different results I look for are: A-QW,A-CH,B-FH,C-BW,C-CH,C-AW
I'm looking for combinations.
Can you helpt me?
Use GROUP BY clause:
SELECT Column1,Column2 FROM tblname GROUP BY Column1,Column2
Grouping will do what you need. It will conveniently eliminate the duplicates for you.
select column1, column2
from <your_table>
group by column1, column2
Pretty sure this will do what you want. demo here
update based on comment
select count(*) from (
select column1, column2
from <your_table>
group by column1, column2
) q
updated demo
In some cases, we can use an expression which combines the two columns...
As an example:
SELECT COUNT(DISTINCT CONCAT(t.column1,'-',t.column2)) AS cnt_combinations
FROM mytable t
This only works in the special case, where we know that a dash character isn't included as a character in column1 or column2. Adding that delimiter character isn't strictly necessary either.
In the more general case, this wouldn't produce the "correct" results if the contents of the columns included values like.
column1 column2 CONCAT(column1,'-',column2)
------- ------- ---------------------------
A -CW A--CW
A- CW A--CW
Since the result of the expression is the same value, those won't be seen as distinct values.
The same thing happens with NULL values, because CONCAT() will return NULL whenever any of the argument values is NULL.
For example, using the string NULL to represent a NULL value
column1 column2 CONCAT(column1,'-',column2)
------- ------- ---------------------------
NULL CW NULL
A NULL NULL

mysql query sort database

Hello I have a table with 4 column in the column 4 the value come from a list box already pre establish by me , let say column 4 is populate by value a,b,c,d,e,f,g,h,i,j.
presently I can sort them in alpha order and get that on a PHP page .
I will like to be able to get on a page just d,e and f
how can I achieve that
thank you all
SELECT * FROM mytable WHERE mycolumn4 IN ('d','e','f')
You can just do:
SELECT column4
FROM TABLE
WHERE column4 IN ('d','e','f')
ORDER BY column4 ASC;
or if you know you want the elents with letters "bigger" than 'd' but don't know how many they are, you can do:
SELECT column4
FROM TABLE
WHERE column4 >= 'd'
ORDER BY column4 ASC;