Pull recrods which is not available in another table codeigniter - php

I am looking for query result where I can see only Table 1 data which is not in Table 2; Here is my table definition and data;
Table 1
id name father name age
1 a a father 60
2 b b father 70
3 c c father 60
4 d d father 50
5 e e father 20
6 f f father 32
7 g g father 40
Table 2
id account_amount
1 42
3 90
5 80
7 49
Now I want all those records from Table 1 which is either not available in Table 2 or its correspondence account_amount in Table 2 is less than 50. Here would be the required outout
id name father name age
1 a a father 60
2 b b father 70
4 d d father 50
6 f f father 32
7 g g father 40
Thanks in advance for solution query in codeigniter

With anti join:
SELECT t1.id, t1.name, t1.father_name, t1.age
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id AND t2.account_amount >= 50
WHERE t2.id IS NULL
With not exists:
SELECT t1.id, t1.name, t1.father_name, t1.age
FROM table1 t1
WHERE NOT EXISTS
(SELECT 1 FROM table2 t2 WHERE t2.id = t1.id AND t2.account_amount >= 50)
Since you tagged the question with MySQL the first option gives a little gain in performance.
In the model
$this->db->query("SELECT t1.id, t1.name, t1.father_name, t1.age
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id = t2.id AND t2.account_amount >= 50
WHERE t2.id IS NULL");
Imo Active Record is inconvenient for this kind of queries

Here you are:
$this->db->select('id');
$excludedData = $this->db->get('table2')->result_array();
foreach($excludedData as $key => $record){
$excludedData[$key] = $record['id'];
}
$this->db->where_not_in('id',$excludedData);
$desiredData = $this->db->get('table1')->result_array();

Related

left join and return only the latest record from right table using LARAVEL 6.5.0

Table 1
----------
NameID Name
------------------
1 A
2 B
3 C
-----------------
Table 2
--------------------
ID NameID Order
--------------------
1 1 Sugar
2 1 Salt
3 2 Garlic
4 2 Sugar
5 2 Onion
6 3 Oil
7 3 Black pepper
I want to return only the latest and only one record per nameID from
right table I dont know what code to use
This is the Result I want to return
RESULT
----------------------------
Name Order
---------------------------
A Salt
B Onion
C Black pepper
Controller.php
return DB::table('table1')
->leftjoin('table2','table1.nameID','=','table2.nameID')
-get();
try this
$data = Table1::select('Table1.Name', 'Table2.Order','Table2.ID')
->leftJoin('Table2', function($query) {
$query->on('Table1.NameID','=','Table2.NameID')
->whereRaw('Table2.ID IN (select MAX(a2.ID) from Table2 as a2 join Table1 as u2 on u2.NameID = a2.NameID group by u2.NameID)');
})->get();
Edited :
$data = Table1::select('Table1.Name', 'Table2.Order','Table2.ID')
Use not exists to filter
select Name, Order
from Table1 a
inner join
(
Select a.NameID, Order from Table2 a
where not exists(select 1 from Table2 b where a.NameID = b.NameID and a.ID < b.ID)
)b on a.NameID = b.NameID
You can try this below script-
SELECT B.NameID, B.Name, C.[Order]
FROM
(
SELECT Nameid,MAX(ID) ID
FROM table_2
GROUP BY NameID
)A
INNER JOIN Table_1 B ON A.NameID = B.NameID
INNER JOIN Table_2 C ON A.NameID = C.NameID AND A.ID = C.ID

How to update single rows in duplicate entries

I have two tables t1 and t2
t1->
id line Amt
----------- ----------- -----------
1 1 0
1 2 0
2 1 0
2 2 0
2 3 0
3 3 0
3 4 0
3 5 0
4 2 0
4 3 0
--------------------------
t2->
id amt
----------- -----------
1 500
2 350
3 750
4 400
In this case I need to update t1 with amount from t2. But I need to update only one row for each id on minimum line. I can do it in MSSQL using the following query-
update a set a.amt=c.amt from T1 a inner join (
select id,min(line) line from T1 group by Id) b
on a.id=b.id and a.line=b.line
Inner join T2 c on a.id=c.Id
I want to do it in MYSQL. Is there any idea to do something like this
You need to tweak your syntax remove from clause, move set clause after joins part
update T1 a
inner join (
select id,min(line) line from T1 group by Id
) b on a.id=b.id and a.line=b.line
inner join T2 c on a.id=c.Id
set a.amt=c.amt
DEMO

Make condition on MySQL table LEFT JOIN "ON"

Here is my left join query:
SELECT *
FROM `table1` as t1
LEFT JOIN `table2` as t2
on t2.table1_id = t1.id // here joins all id,s of table1 in table 2.
where t1.master_id = 72
Here I want joins only like t2.table1_id = first id of t1.id fetched from t1.master_id.
returning TABLE
ID master_id t1_name t2_name
3 72 A A1
3 72 B A2
6 72 C A3
6 72 D A4
EXPECTING TABLE
ID master_id t1_name t2_name
3 72 A A1
3 72 B A2
6 72 C
6 72 D
expecting table returns only first id based result in column t2_name!
If the first id (first array id element) of t1.id = 3 then query look like this:
SELECT *
FROM `table1` as t1
LEFT JOIN `table2` as t2
on t2.table1_id = t1.id AND t2.table1_id = 3
where t1.master_id = 72
but how we made this dynamically with single query?
You could use a case to check if the master_id and ID are equal. This statement returns the t2_name when ID and master_id are equal, otherwise it returns just an empty string. This might not be the fastest option when you use this query on a large database.
I hope this is what you are looking for :)
SELECT ID, master_id, t1_name, t2_name =
CASE ID
WHEN master_id THEN t2_name
ELSE ""
END
FROM 'table1' as t1
JOIN 'table2' as t2
ON t2.table1_id = t1.id
WHERE master_id = 72
If you perform a LEFT JOIN, You will only get NULL (empty slots) instead of A3 and A4 IF there is no match on table2 for the last two rows of table1. It would be very helpful if you could show us some of the data in both tables.

mysql query error matching data

I'm getting an error in sql
I have three tables
T1, t2, t3
in t1
id name
1 a
2 b
3 c
4 d
5 e
in t2
t1_id name
4 sac
2 sau
4 rah
4 seh
1 kaif
5 zah
6 aas
8 ram
in t3
t1_t2_id name count lif_lin
1 Eve 2 no
2 sun 1 no
3 mon 0 no
4 tue 3 no
5 wed 1 no
6 thu 1 no
I want to count the how much t1_id element's exist in t1 respectively of id, t1_id in t1_t2_id
Mean I have category id in t1 and the same category id in t2 table with t1_id and t3 t1_t2_id and i want to count the how many matches found in t2 with the same t1_t2_id
Not sure I exactly understand what you are saying but try:
SELECT count(a.id)
from t1 a
join t2 b on a.id = b.t1_id
join t3 c on a.id = c.t1_t2_id
I think this will work for you (number if t1 ids in t2):
SELECT count(*) from t1,t3 where t1.id=t2.t1_id;

MYSQL/PHP sum with group by

tableA
id B_id C_id
1 1 0
2 1 0
3 0 1
4 0 2
5 0 2
6 0 2
tableB
id amount quantity
1 10 2
tableC
id amount quantity
1 6 1
2 15 3
I have this kind of database and I know it is not structured very well because I only continued this website and I wasn't given much time to restructure the website.
My question is how can i get the total amount of tableB and tableC using a LEFT JOIN of the two tables to tableA. As you can see the quantity in tableB and tableC will make the same number of tableA record. I was able to get each of the transactions amount using this code:
SELECT *
FROM tableA A
LEFT JOIN tableB B ON A.id = B.id
LEFT JOIN tableC C ON C.id = A.id
GROUP BY B.id, C.id
It would return:
id B_id C_id B.id B.amount B. quantity C.id C.amount C.quantity
1 1 0 1 10 2 0 0 0
3 0 1 0 0 0 1 6 1
4 0 2 0 0 0 2 15 3
but now I want to get the total using mysql SUM but GROUP BY cannot be used with the aggregate function SUM so the total should be: 10+6+15 = 31
Is this what you want?
SELECT coalesce(sum(b.quantity), 0) + coalesce(sum(c.quantity), 0)
FROM tableA A LEFT JOIN
tableB B
ON A.id = B.id LEFT JOIN
tableC C
ON C.id = A.id;
This returns the total across the two tables.
EDIT:
If you just want the sum of tables b and c, what is table a for? Is this what you want?
select amountb + amountc
from (select sum(amount) as amountb from tableb) b cross join
(select sum(amount) as amountc from tablec) c;

Categories