Make condition on MySQL table LEFT JOIN "ON" - php

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.

Related

I have two tables which contains duplicate values and unique values. I don't need duplicate values from both the tables, I need only unique values

I have two tables which contains duplicate values and unique values I don't need duplicate values from both the tables I need only unique values and copy it into a new table as output.
For ex:
Table 1 data
col1
101
102
103
104
Table 2 data
col1
101
102
103
105
Output required is
New Table 3 data
col1
104
105
The query is
SELECT
Table 1 data.col1
FROM
Table 1 data
LEFT JOIN
Table 2 data
ON
Table 1 data.col1 = Table 2 data.col1
WHERE
Table 2 data.col1 is NULL
Hope this will help :
(SELECT t1.col1 FROM t1 left join t2 on t1.col1 =t2.col1 WHERE t2.col1 is null) union (SELECT t2.col1 FROM t2 left join t1 on t1.col1=t2.col1 WHERE t1.col1 is null)
Use the Below Query with UNION:
(SELECT Table1.column1 FROM Table1 left join Table2 on Table1.column1 =Table2.column1 WHERE Table2.column1 is null)
union
(SELECT Table2.column1 FROM Table2 left join Table1 on Table1.column1=Table2.column1 WHERE Table1.column1 is null)
get the distinct value from both table (use UNION) to produce
101 102 103 104 105
then get the intersect data from both table (use INNER JOIN) to produce
101 102 103
after that, subtract result 2 from result 1 (in Oracle you can use MINUS operator, but you can use the same idea to build complex query for that in mysql using NOT IN operator)
Hope this will work. I tried in Oracle Database. hopefully It will work in MYSQL also.
SELECT col1
FROM tab1 outers
WHERE NOT EXISTS
(SELECT col1 FROM tab2 inners WHERE outers.COL1 = inners.COL1)
UNION
SELECT col1
FROM tab2 outers
WHERE NOT EXISTS
(SELECT col1 FROM tab1 inners WHERE outers.COL1 = inners.COL1);
INSERT INTO Table 3 data (clo1) SELECT CombinedValue FROM ( SELECT DISTINCT col1 AS CombinedValue FROM Table 1 data UNION ALL SELECT DISTINCT col1 FROM Table 2 data ) temp GROUP BY CombinedValue HAVING COUNT(*) =1
This query worked for me :)
Thank you all for your help

SQL result should show all table one data

I have two tables as shown below:
table_one
teamid teamname description
1 x abcd
2 y dcba
3 z sadf
table_two
stageid teamid responses score
1 1 r1 20
1 2 r2 30
2 1 r4 20
2 2 r5 20
2 3 r6 20
I am trying to join the above two tables based on stageid number which I have. So, I am tried the following:
SELECT t1.teamid, t1.teamname, t2.responses, t2.score
FROM table_one as t1
JOIN table_two as t2 ON t1.teamid = t2.teamid
WHERE stageid = 1
Which is giving me following result (I tried all combinations of left, right, inner, outer joins):
teamid teamname responses score
1 x r1 20
2 y r2 30
Expected result table
teamid teamname responses score
1 x r1 20
2 y r2 30
3 z NULL 0
As you can see in the above expected table, I want all the rows of the table_one and from table_two if the data isn't present, I need NULL or 0 as values.
How to do this?
Try this:
SELECT t1.teamid, t1.teamname, t2.responses, t2.score
FROM table_one as t1
LEFT JOIN table_two as t2 ON t1.teamid = t2.teamid
WHERE stageid = 1 OR stageid IS NULL
By default when you use left join and there is nothing to join the fields gonna contain NULL, so you have to add NULL not just a specific stageid.
Or as others menthioned you can set the stageid like this:
SELECT t1.teamid, t1.teamname, t2.responses, t2.score
FROM table_one as t1
LEFT JOIN table_two as t2 ON t1.teamid = t2.teamid AND stageid = 1
WHERE 1
In this query you use the ON tag of the join to set the stageid and you get the same result. (WHERE 1 is not necessary)
Check JOINs: https://stackoverflow.com/a/6188334/2231168
If you want to retrieve the non-matching record also then use LEFT JOIN instead of INNER JOIN.
Query
select t1.teamid, t1.teamname, t2.responses, coalesce(t2.score, 0) as score
from table_one t1
left join table_two t2
on t1.teamid = t2.teamid
and t2.stageid = 1;

get all rows if there is only few row match in sql

I have the following two tables:
Table 1:
student_id Name
...................
1 Kumar
2 kishan
3 Mohan
4 Kanha
5 Murat
Table 2:
student_id is_attend
.........................
1 1
4 1
Table 2 represents the students who have an attended status.
The following is the result we would like to get. So that we can show through PHP the attendance of each student.
Result:
student_id is_attend
..........................
1 1
2 0
3 0
4 1
select T1.student_id,T2.is_attend from
table1 T1 left join
table2 T2 on
T1.student_id=T2.student_id;
Use Left join as below :
Select Table1.*,Table2.is_attend
from Table1
left join Table2
on Table1.student_id = Table2.student_id
You can use sub-query also :
select s.student_id,
(select IFNULL(is_attend,0) from attend where student_id=s.student_id) as is_attend
from student as s
You can use the following query:
SELECT t1.student_id,
t1.Name,
IFNULL(t2.is_attend,0)
FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.student_id = t2.student_id
You can see this here -> http://sqlfiddle.com/#!9/86511/2
Hope this helps!!!

Multiple WHERE conditions in 2 tables in MySQL

I have 2 tables as follows:
table1
ID Name Test
A011 John 1
A012 Lynda 1
A013 Micheal 1
A014 Jack 0
A021 Joe 1
A015 Paul 0
table2
ID Done
A011 1
A012 1
I want to select all rows from table1 that have an ID where the 3 first letters are equal to A01, and the test field is 1, and also the ID is not present in table2.
I tried this query:
SELECT a.* FROM table1 a LEFT JOIN table2 b ON a.ID = b.ID
WHERE a.test = 1 AND b.ID IS NULL
The result from that is 2 rows with ID A013 and A021. I tried to use LEFT(ID,3) to get the ID with A01, however, I couldn't achieve what I want.
How can I filter only the records where the ID starts with A01?
Try this, it will give you the desired result
SELECT t1.* FROM table1 t1 LEFT JOIN table2 t2 ON t1.userid = t2.userid WHERE LEFT(t1.userid , 3) LIKE '%A01%' AND t1.userid NOT IN (SELECT userid from table2)
SELECT * FROM table1
WHERE test = 1
AND ID LIKE "AO1%"
AND ID NOT IN (SELECT ID from table2)

Pull recrods which is not available in another table codeigniter

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();

Categories