I am trying to do a conditional case statement and am not sure if sql can perform this.
I need to select the ID from Table A, and the Name from the corresponding ID in table B. If the Name in Table B is NULL, i need to select the Name from Table C. I am not sure how to do this using a CASE or if it is possible, any help is greatly appriciated.
Table A Table B Table C
ID ID2 ID2 Name ID2 Name
________ ___________ ___________
1 3 1 bill 1 NULL
2 2 2 steve 2 NULL
3 1 3 NULL 3 george
Use IFNULL() to select the appropriate column from B or C.
SELECT a.id, IFNULL(b.name, c.name) AS name
FROM a
JOIN b ON a.id = b.id2
JOIN c ON a.id = c.id2
If the matching rows could be missing from B or C, use LEFT JOIN instead of JOIN.
Try the following query:
select IFNULL(TableB.name, TableC.name) as name
from TableA
left join TableB TableA.ID2=TableB.ID2
left join TableC TableA.ID2=TableC.ID2
I was too slow :p
Here is a fiddle, if it might help someone: http://sqlfiddle.com/#!9/b28c5/2
I assumed that each ID2 value is present in either both or none of TableB/TableC.
Related
Table A
a_id
a_name
1
apples
2
bananas
3
cherries
4
oranges
Table B
b_id
b_name
1
Hot
2
Cold
Table C
c_id
a_id
b_id
1
1
2
2
2
1
3
3
2
4
4
1
5
4
2
I am trying to get resulting table, which should show "a_name" and "b_name" with the following condition: where a_name like '%r%' and b_name like '%o%'.
Problem is that they should be found within the same row in "Table_C".
I've tried various methods of joining tables but I'm unable to get the desired result.
Here's my best coding attempt at this problem:
SELECT a.a_name,
b.b_name
FROM Table_A a
WHERE a.a_name LIKE '%r%' IN (SELECT a.a_id
FROM Table_c
WHERE b_id LIKE '%o');
Any help would be much appreciated.
The problem with your query is that you're trying to extract values of "Table B" by using a filtering operation (WHERE clause). As long as the filtering clause "just filters" - reduces the number of rows of your table - , you can't access "b.b_name" inside your SELECT clause if the FROM contains only "Table A".
In this case you may want to use two JOIN operations in place of WHERE .. IN .. construct. The main table is "Table C" because it connects "Table A" and "Table B" together. Since you have two conditions to be applied on the names, in order to make this more efficient, you can filter on the two tables ("Table A" and "Table B") before applying the JOIN operation. In this way the DBMS will apply the matching on less rows.
SELECT A.a_name,
B.b_name
FROM tabC C
INNER JOIN (SELECT * FROM tabA WHERE a_name LIKE '%r%') A
ON C.a_id = A.a_id
INNER JOIN (SELECT * FROM tabB WHERE b_name LIKE '%o%') B
ON C.b_id = B.b_id
Check the demo here.
select
A.a_name,
B.b_name
from TableA A
inner join TableC C on C.a_id = A.a_id
inner join tableB B on B.b_id = C.b_id
where A.a_name like '%r%' and B.b_name like '%o%';
The inner join 'glues' the table together on the condition specified after the ON
The where clause is a copy provided by you. I Just added the table aliases for clarity.
Try below code:
select tableA.a_name, tableB.b_name
from tableC
left join tableA on tableA.a_id=tableC.a_id
left join tableB on tableB.b_id=tableC.b_id
where tableA.a_name like '%r%' and tableB.b_name like '%o%'
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
One table A, looks like this:
table A:
==========
ID NAME
1 Ted
2 John
3 Sandy
4 Robert
5 Helen
table B:
=========
CONTRIBUTION CONTRIBUTOR_ID
100 1
200 3
150 3
270 2
30 1
Assuming table B is very big and table A is small, I would like to pseudo iterate on this.
- take first ID from table A
- search for the first occurrence in table B, if found add to result
- if not continue to next ID in table A.
- repeat until end of table A
I would like a list of all ID's from table A that exist in table B
So the result here would be:
1
2
3
Of course, the tables are properly indexed.
any idea how to write this efficiently in MySQL?
Thanks
Or just simply
select distinct contributor_id
from table B
select distinct ID from tableA inner join tableB
on table.ID=tableB.CONTRIBUTOR_ID
Try this:
select tA.ID
from tableA tA inner join tableB tB on tA.ID = tB.CONTRIBUTOR_ID
group by tA.ID
the query
SELECT * from A,B where A.ID = B.CONTRIBUTOR_ID
would select all rows with an existing ID in both tables. It would leave out all rows in A which never have contributed (not exist in B)
EDIT:
to only get the IDs of those who ever contributed, try:
SELECT ID FROM A WHERE EXISTS (SELECT CONTRIBUTOR_ID FROM B where ID = CONTRIBUTOR_ID)
If table B can contain contributors that don't exist in table A, then I suggest trying:
select tA.ID
from tableA tA
inner join (select distinct contributor_id from tableB) tB
on tA.ID = tB.CONTRIBUTOR_ID
(assuming you have an index on Contributor_ID on tableB)
Can be done by using subquery
Select distinct ID from A where ID in(select CONTRIBUTOR_ID from B)
Table A
Name Value
John 1
Mary 2
Gary 3
Table B
Name Value
Jim 10
Jason 20
Mary 30
I want Name and Value from Table A, but overriding Value if exists in Table B. So my expected output would be:
John,1
Mary,30
Gary,3
I was trying something like:
SELECT A.Name, IF(EXISTS(B.Value),B.Value,A.Value) FROM Table A LEFT JOIN Table B on B.Name=A.Name
You can use the Mysql COALESCE function for this. It will return the first non-null argument in a list of parameters:
So your code would look something like:
SELECT A.Name,
coalesce(B.Value,A.Value)
FROM Table A LEFT JOIN Table B on B.Name=A.Name
So what would happens is that if there is a value in table B, that will be used, if not, it will revert to A.
See the docs: http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_coalesce
Try this instead:
SELECT
A.Name,
CASE WHEN b.Name IS NULL THEN a.Value ELSE b.value END AS value
FROM TableA AS a
LEFT JOIN TableB AS b on B.Name=A.Name;
SQL Fiddle Demo
Hi Guys I have two tables table B has a many to 1 relationship with table A
TableA TableB
id name id value
1 basketA 1 10
2 basketB 1 5
1 7
2 7
2 3
etc..
now
$query = $this->db->get('TableA');
return $query->result_array();
returns the A fields obviously but how can I do a join so it will return A-Field along with the sum of the B-Items for that field?
eg. in the result array
BasketA 22
BasketB 10
Thanks in advance!
try this:
select A.id,A.name,SUM(B.value)
from TableA A join tableB B
on A.id=B.id
group by A.id,A.name
This should work:
SELECT A.name, SUM(B.value) AS sum_value
FROM TableA A
INNER JOIN tableB B
ON A.id=B.id
GROUP BY A.id;