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;
Related
I am trying to write a query which retrieves data from a table which doesn't have data on another table.
I have table A with values
ID StudentID CLASSID
1 1 2
2 2 3
3 3 4
4 4 5
TABLE B with values
ID StudentID CLASSID
1 1 2
2 2 3
I am trying to return values from table A with ID 3,4 which is not available in TABLE B.
Query I have tried is
SELECT *
FROM A AS a
WHERE NOT EXISTS
(
SELECT *
FROM B AS b
WHERE a.student_id = b.student_id
AND a.CLASSID = b.CLASSID
);
NOTE: As my problem was slow query. I have fixed this problem by creating index which made this query run fast.
Thanks for your effort.
Using LEFT OUTER JOIN
SELECT TableA.* FROM TableA
LEFT OUTER JOIN TableB
ON TableA.ID = TableB.ID
WHERE TableB.ID IS null
Using NOT IN
SELECT * FROM TableA
WHERE TableA.ID NOT IN ( SELECT ID FROM TableB)
Using NO EXISTS
SELECT *
FROM tableA a
WHERE NOT EXISTS
(
SELECT 1
FROM tableB b
WHERE a.studentsID = b.studentsID
AND a.CLASSID = b.CLASSID
);
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.
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)
Following is my tables which is student id is common field in both tables. I want to get both tables data in single query.Also get recent data of student.
table A:
student_id name surname email
------------------------------------------------
1 ABC LLL abc#gmail.com
2 PQR SSS pqr#gmail.com
Table B:
student_id Assignment_Id Assignment_Name last_submited
---------------------------------------------------------------------
2 1 asign_1 sub_0001
1 2 asign_2 sub_0002
2 3 asign_2 sub_0003
I want exact output like:-
student_id Assignment_Id email last_submited
--------------------------------------------------------------
2 3 pqr#gmail.com sub_0003
I have used following query for getting recent record but confused how to get email id along with this.
SELECT assignment_id,
student_id,
last_submited
FROM tableB
WHERE student_id= '2'
ORDER BY assignment_id DESC LIMIT 1
You can make use of a JOIN
select tableB.assignment_id,
tableB.student_id,
tableB.last_submited,
tableA.email
from tableB INNER JOIN
tableA ON tableB.student_id = tableA.student_id
where tableB.student_id= '2'
order by tableB.assignment_id desc
limit 1
INNER JOINs are used to return data where the data is in both tables (so en entry would exist in tableA and tableB).
LEFT JOINs are used when you wish to retrieve all data from tableA and those values that are available in tableB.
So, lets say you had
TABLEA
-------
1
2
and
TABLEB
-------
1
SELECT *
FROM TABLEA INNER JOIN
TABLEB ON TABLEA.ID = TABLEB.ID
would return
1,1
Whereas
SELECT *
FROM TABLEA LEFT JOIN
TABLEB ON TABLEA.ID = TABLEB.ID
would return
1,1
2,NULL
You need to use join
SELECT a.student_id,
b.Assignment_id,
a.email,
b.last_submitted
FROM a
INNER JOIN b ON a.student_id = b.student_id
WHERE a.student_id= '2'
ORDER BY b.assignment_id DESC LIMIT 1
Join both the tables,
SELECT B.assignment_id,
B.student_id,
A.email_id ,
B.last_submited
FROM tableB 'B',
tableA 'A'
WHERE B.student_id= '2'
AND A.student_id=B.student_id
ORDER BY assignment_id DESC LIMIT 1