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
Related
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
In my table I have same dates but with different Invoice_no_pawl varchar2 and Null. How to show the 1200.00 in the first place and of the date?
This is the query:
select invoice_date, invoice_no_pwal, invoice_value, payment_rec
from partywise_accounts_ledger
where name_of_the_customer = 'Naresh Agencies'
ORDER BY column1 DESC, column2
This sorts everything by column1 (descending) first, and then by column2 (ascending, which is the default) whenever the column1 fields for two rows are equal.
Best of luck!
Reference: Here
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;
I am able to replicate my issue with a very simple case..
Explanation
I have a very simple table my_table with one column column1.
create table my_table (column1 varchar(58));
I have few values for this column, NULL is also one of them.
insert into my_table (column1) values ('value1'), ('value1'), ('value2'), (null), ('value2');
Problem
When I try to query for group by column1 It is giving expected results by grouping all NULLs together. However if I add a where clause on column1 something like
select count(1) as value_count, column1 from my_table where column1 <> 'value1' group by column1;
It is ignoring both value1 and NULL where I was expecting to ignore only value1.
With this simple case I could get a workaround for this by adding an OR condition, But it is a real pain to add this condition all over in my original case.
Could someone can explain me better why this behavior and how can I fix this?
This is because any comparison with a NULL does not produce a true or false result, but instead produces a NULL result. Consequently, the condition column1 <> 'value1' evaluates as NULL where column1 is NULL, and so NULL values are not selected.
You can get around this by using a function such as coalesce to test column1 - like so:
select count(1) as value_count, column1
from my_table
where coalesce(column1,'') <> 'value1'
group by column1;
Null means "a value, but I don't know what it is".
So is column1 <> 'value1'? If column1 is null, then "Is a value, but I don't know what it is, unequal to 'value1'"?
Clearly the answer is "I don't know. I don't know what the value is".
The only rows included by a where clause are those which pass the where clause test. We don't know if this row passes the test, so it will not be included in the query.
You could try making use of the MySQL IFNULL in your predicate:
select count(1) as value_count, column1 from my_table where ifnull(column1,'nullvalue') <> 'value1' group by column1;
So i'm looking to write a MySQL query that will return a result set that, when a particular column has a particular row value, it will return that row instead of another near duplicate row but otherwise return results like normal.
Okay, here is my table
id name value another
1 name1 value1
2 name1 value1 foo
3 name2 value2
4 name3 value3
and results should be (if foo is present):
id name value another
2 name1 value1 foo
3 name2 value2
4 name3 value3
I did find this example: MySQL get rows but prefer one column value over another but couldn't figure out how to adapt it to my needs...
I hope I'm making sense! No sleep in two days ain't good for attempts at elucidation! Also I'm very sorry if this has already been asked, i searched for a good long time but just didn't have the vocabulary to find any results...
Thanks in advance!
SELECT
a.*
FROM atable a
LEFT JOIN atable b ON a.name = b.name AND a.another = 'foo'
This will filter out rows with an empty another, for which an entry with the same name and value exists that does have another.
select *
from YourTable yt1
where not exists
(
select *
from YourTable yt2
where yt1.id <> yt2.id
and yt1.name = yt2.pname
and yt1.value = yt2.value
and yt1.another = ''
and yt2.another <> ''
)
This sounds like a situation where the mysql coalesce function would be handy.
Coalesce returns the first non-null parameter it's given. So you can use,
SELECT id, COALESCE(another, value) FROM MyTable;
this will return two columns, the id field and either the contents of the "another" column (if it is not null) or the contents of the "value" column.