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')
Related
I have 2 tables in mySQL DB: A & B.
a, b, c columns.
Table A:
a(1) = 1
a(2) = 2
a(3) = 3
Table B:
a(1) = 1
a(2) = 2
So, we could see that in B table there is no row with a = 3. How could I request DB to find it?
So response (one row) could looks like:
a(1) = 3
b(1) =..
c(1) =..
One option uses EXISTS:
SELECT a.a
FROM TableA a
WHERE NOT EXISTS (SELECT 1 FROM TableB b WHERE b.a = a.a);
Another option would be to do an anti-join:
SELECT a.a
FROM TableA a
LEFT JOIN TableB b
ON a.a = b.a
WHERE b.a IS NULL;
You can also give up joins and use WHERE and nested SELECT:
suppose TabA holds values of 1,2,3 in subsequent rows of column ValA
and TabB holds values of 1,2 in subsequent rows of column ValB
and you want only a row containing value of 3 from TabA
you can do this without joins:
SELECT Val_A
FROM TabA
WHERE Val_A NOT IN (SELECT Val_B FROM TabB)
I am using a simple query:
select A,B from Table1 where id in ('');
which gives me output like:
A B
1 X
2 V
3 R
Now i want to know count of value B in whole database:
i.e
A B CountB
1 X 3
2 V 1
3 R 2
This ought to to it:
SELECT *
FROM (SELECT A,
B
FROM Table1
WHERE id in ('')) t1 LEFT JOIN
(SELECT B,
COUNT(B)
FROM Table1
GROUP BY B) t2 ON t1.B = t2.B
May I didn't understand the question but it seems the following query should do:
SELECT A,
B,
COUNT(*)
FROM Table1
WHERE .....
GROUP BY A,
B
ORDER BY A,
B;
You need to make use of group_by if you want certain column, and then add a count the id of it to get count. So I'd do
SELECT A,B,COUNT(B.ID) as CountB FROM Table1 WHERE id IN ('') GROUP BY B;
I am building a SQL query which compares two tables A and B by a [name] column and returns the names from table A that are not in table B
Example
Table A
ID Name Address
1 A ABC
2 B XYZ
3 C PQR
Table B
ID Name Gender
1 A F
2 B M
3 D F
The query I wrote should return third row from table A as it is not in table B and should exclude all other rows
Following is the query I built
Select * from A oa left join B gp ON oa.name!=gp.name
the above doesn't return the results I was expecting.
Can this be corrected?
Easiest way:
select * from A where name not in (select name from B)
Better way:
select * from A where not exists (select 1 from B where B.name = A.name)
"A left join B" means keeping everything in A, and associating records in B if the condition is satisfied.
In your case, if you really wanna use left join, here is what it should be ('=', not '!='):
Select * from A oa left join B gp ON oa.name=gp.name where gp.name is null
Better way would be using 'not exists' performance-wise, or 'except' if null values are not an issue.
Using excpet operator will help
select * from TableA
except
select * from TableB
SELECT a.*
FROM A a
LEFT JOIN B b
ON a.name = b.name
WHERE b.name IS NULL
If I have a table like this:
id car
1 A
1 B
1 C
1 D
2 A
2 B
2 C
2 F
3 A
3 C
3 E
3 F
3 G
what I want is different "id" which have ("A" or "C") and "B" in car. For example:
id car
1 A
1 B
1 C
2 A
2 B
2 C
what I did was
select * from table where (car like "A" or car like"C") and (car like "B")
but it gives me an empty row.
Any clue?
You can use a self-join
SELECT t1.id
FROM yourTable AS t1
JOIN yourTable AS t2 ON t1.id = t2.id
WHERE t1.car IN ('A', 'C')
AND t2.car = 'B'
BTW, you should generally only use LIKE when you're doing a pattern match. For exact matches use =, or IN for matching any of multiple items.
Untested, but something like this should work: get all rows that have the ID such that it is found on the list of all IDs that have an A or a C, and also on the list of all IDs that have a B.
SELECT t.*
FROM mytable t
WHERE t.id IN (
SELECT DISTINCT id
FROM mytable t2
WHERE t2.car='A' OR t2.car='C'
)
AND t.id IN (
SELECT DISTINCT id
FROM mytable t3
WHERE t3.car='B'
)
I have two tables, lets called them A and B. Here is what they look like
Table A:
ID BCODE
1 A1
2 B1
3 C1
4 D1
5 F1
Table B:
X Y IDX IDY
A1 D1
D1 F1
C1 B1
Table B has columns 'X' and 'Y' that have values that are found in Table A's column BCODE.
I want to insert the ID from Table A for each joined X,Y column with Table A's BCODE. Each matching BCODE for X goes to IDX. Each matching BCODE for Y goes to IDY. This is what it should look like
Table B:
X Y IDX IDY
A1 D1 1 4
D1 F1 4 5
C1 B1 3 2
I have a table A and B with about 50 million rows in both Table A and B
Does anyone know how I can do this ? I tried using INNER JOINS but it didnt populate all the columns. Thanks
you need a simple join with the Update command:
in MS SQL Server:
Update tableB
set
tableB.IDX=a.ID,
tableB.IDY=aa.ID
from
tableB
join tableA a on tableB.X=a.BCODE
join tableA aa on tableB.Y=aa.BCODE
in MySQL:
Update tableB
join tableA a on tableB.X=a.BCODE
join tableA aa on tableB.Y=aa.BCODE
set
tableB.IDX=a.ID,
tableB.IDY=aa.ID
(Edit: as question is about MySQL I just create The DEMO for it)
in Oracle PL/SQL:
Update (select tableB.IDX idx, tableB.IDY idy, a.ID ida, aa.ID idaa
from tableB
join tableA a on tableB.X=a.BCODE
join tableA aa on tableB.Y=aa.BCODE)
set
idx=ida,
idy=idaa
UPDATE b
SET idx =
(SELECT id
FROM a
WHERE bcode = b.x),
idy =
(SELECT id
FROM a
WHERE bcode = b.y);