How do I get distinct count values of 2 columns? - mysql

Lets say I have 2 columns, a and b.
Column A contains these values:
a
b
c
d
Column B contains these values:
e
d
c
b
a
What can I do to get the absolute distinct values from BOTH columns?
Meaning if i want to have a count on both columns, the result is to be 5. Because a, b, c and d are repeated in column B but not e.

SELECT A
FROM YourTable
UNION
SELECT B
FROM YourTable

Related

How to complete this query function?

I have three tables.
Table A ###(Code, Value are combined primary key)
Code Value
1 | b
1 | c
3 | c
Table B
Value
b
Table C
Value
c
I would like to write a query of 'Code' from Table A.
The condition is that 'Code' should contain Value 'c'.
If 'Code' contains Value'b', this Code shouldn't be queried. (that's, Code 1 has value b and value c, so Code 1 needs to be excluded)
But I'm not available to do that query.
The expected outcome might be '3'
I want to use intersect but MySql doesn't contain this function yet.
So I tried some codes.
I'm sure that my codes have problems but I have no idea how to fix it.
SELECT DISTINCT A.*
FROM A B C
WHERE A.Value IN
(SELECT Value FROM B)
AND A.Value NOT IN
(SELECT Value FROM C);
Could you give me some tips on my questions?
the problem is that you are joining a, b and c. you almost got it.
select distinct a.value
from a
where a.value in (select value from b)
and a.value not in (select value from c)
or you can use join
select *
from a
inner join b where b.value = a.value
and not in (select value from c)

Multiplying two columns In Access Query

I want to multiply two columns in a query
Example : Column A * Column B and have the result be displayed in Column C
I assume you have a table YourTable containing the columns A and B and that the columns A and B also should be displayed in the resulting query.
So here it is:
Select A, B, A * B As C From YourTable
If not, use just this:
Select A * B As C From YourTable

Subtract rows from a table, using subquery which may be empty

table: A
-----------
value
1
2
3
sub-query: B
-----------
value
2
I need (A - B).
The below query works when B is not empty. Output = (1,3) as expected.
SELECT * FROM A
JOIN B
ON (A.value != B.value)
However, when the sub-query B is empty, the JOIN does an intersection of A with an empty B, and the output is an empty result-set.
And if I use LEFT JOIN, it does not subtract the row containing value 2 from table A.
Is it possible to write a single query for (A - B), irrespective of whether B is empty or not.
SELECT A.*
FROM A
LEFT JOIN B
ON A.value = B.value
WHERE b.value IS NULL

Add a column in SQL query that indicates the number of times that the number is shown in other tables

I need to make a query that selects everything from a table A, and in addition to have a column that indicates the number of times that the value of A.col1 is in B.col2.
Example:
Table A:
id name
1 "y"
2 "z"
3 "w"
Table B:
id name
15 "y"
23 "w"
14 "y"
I want a query that will give me the following:
id name numOfTimes
1 "y" 2 // y is shown twice in table B
2 "z" 0 // z isn't shown in table B
3 "w" 1 // w is shown once in table B
try this:
Select a.id, a.name, count(b.id) as numoftimes from a
left outer join b on a.name = b.name
group by a.id, a.name;
It can be done in multiple ways
select a.id as Id
,a.name as Name
,(select count(1) from b where a.id=b.id) as NumberOfTimes
from a;
or
select a.id as Id
,a.name as Name
,count(b.id) as NumberOfTimes
from a
left join b on a.id=b.id;
Try with this following you will get your expected result.
select a.id,a.col1,count(b.col1) as numOfTimes
from TableA a left join TableB b
on a.col1 = b.col1
group by a.id,a.col1;
Thanks.

select from multiple tables

I have table AA with Columns
A B C with values 1,a,a
Table BB with A,B,C with values 2,b,b
if i select B=a and C=a i want to get o/p as Column A with Value 1
or
if i select B=b and C=b i want to get o/p Column A with Value 2
I want single query for this
(SELECT a FROM AA WHERE B = 'something' AND C = 'something')
UNION ALL
(SELECT a FROM BB WHERE B = 'something' AND C = 'something')