Mysql two table, get data without same value - php

I have two table in my DB as table tbl_a and tbl_b which are having following data.
tbl_a
tbl_a_id u_id a_name
1 1 Joe
2 1 Joel
3 1 Joele
4 1 Joelle
5 3 Joeee
tbl_b
tbl_b_id u_id a_name
1 1 Joe
2 1 Joel
3 1 Joele
4 1 Joelle
5 3 Joeee
5 1 Joeees
5 1 Joeeess
How can I get the tbl_b.a_name values which are not present in the tbl_a table as a_name.
My desire output should be like,
Joeees
Joeeess
Right now I am having the following code.
$qqq = $this->db->query("
SELECT
tbl_b.a_name
FROM tbl_b
WHERE tbl.u_id='1'
");
foreach($qqq->result() as $ggg)
{
echo $ggg->a_name;
}
Thank in advance.

The simplest way is to use not exists:
select a.*
from tbl_a a
where not exists (select 1 from tbl_b b where b.b_name = a.a_name);

I got the solution thanks for the help anyways..
SELECT
tbl_b.a_name
FROM tbl_b
WHERE tbl_b.u_id='1'
AND NOT EXISTS (SELECT * FROM tbl_a WHERE tbl_a.u_id='1' AND tbl_b.a_name = tbl_a.a_name)

You can try not in same as follow:
SELECT * FROM `tbl_b`
where a_name not in (select DISTINCT a_name from tbl_a)

select a.*,b.*
from tbl_a a left join tbl_b b on a.u_id = b.u_id
where b.b_name != a.a_name
You can try this it may be useful and execute fast then other query.

Try this
SELECT
DISTINCT tbl_b.a_name
FROM tbl_b where tbl_b.a_name NOT IN (SELECT DISTINCT tbl_a.a_name from tbl_a)

Related

How to join result from two tables with same field into one field Codeigniter?

I use Codeigniter
My invalid SQL query:
SELECT *, (SELECT menu FROM sepetim WHERE SiparisId = S.Id) AS menum
FROM siparisler AS S
WHERE onay = 1 AND BayiId = '1'
ORDER BY Id desc
I have tables like this:
Table1 Table2
Id Id | Text
1 1 name1
2 1 name2
2 name3
Current result:
Wanted result:
Can you help me? Thanx.

PHP - Deleting duplicate values with same ref

I've the following MySQL Table called store
id ref item_no supplier
1 10 x1 usa
2 10 x1 usa
3 11 x1 china
4 12 x2 uk
5 12 x3 uk
6 13 x3 uk
7 13 x3 uk
Now What i'm excepting the output to be is as follows :
id ref item_no supplier
1 10 x1 usa
3 11 x1 china
4 12 x2 uk
5 12 x3 uk
6 13 x3 uk
As you can see item_no x1 and x3 have same ref and supplier source, so what I want is to delete the duplicate record in-order to keep one item_no only !
I've create this PHP code to SELECT results only :
$query1 = "SELECT
DISTINCT(item_no) AS field,
COUNT(item_no) AS fieldCount,
COUNT(ref) AS refcount
FROM
store
GROUP BY item_no HAVING fieldCount > 1";
$result1 = mysql_query($query1);
if(mysql_num_rows($result1)>0){
while ($row1=mysql_fetch_assoc($result1)) {
echo $row1['field']."<br /><br />";
}
} else {
//IGNORE
}
How to tell the query to SELECT Duplicate records properly according to my needs before creating the DELETE query.
Thanks Guys
You can use the following query to produce the required result set:
SELECT t1.*
FROM store AS t1
JOIN (
SELECT MIN(id) AS id, ref, item_no
FROM store
GROUP BY ref, item_no
) AS t2 ON t1.id > t2.id AND t1.ref = t2.ref AND t1.item_no = t2.item_no
Demo here
To DELETE you can use:
DELETE t1
FROM store AS t1
JOIN (
SELECT MIN(id) AS id, ref, item_no
FROM store
GROUP BY ref, item_no
) AS t2 ON t1.id > t2.id AND t1.ref = t2.ref AND t1.item_no = t2.item_no
To find only duplicate records you can use
SELECT * FROM store WHERE id NOT IN
(SELECT id FROM store AS outerStore WHERE id =
(SELECT MAX(id) FROM store AS innerStore
WHERE outerStore.ref = innerStore.ref AND
outerStore.supplier = innerStore.supplier AND outerStore.item_no = innerStore.item_no))
Maybe long, but it should work.
If you want the select of the row to delete use
select * from store
where id not in (
select max(id) from store
group by distinct ref, item_no, supplier);
Or you can directly use a command for direct delete using
delete from store
where id not in (
select max(id) from store
group by distinct ref, item_no, supplier);

SELECT data from two tables with same name

I've tried to find a way to select data from two tables within the same query as follows:
Suppose I have this as table1
id | item | qty
1 | 1bb1 | 12
2 | 1cc1 | 10
and as table2
id | item | qty
6 | 1bb1 | 12
7 | 1vv1 | 4
And I have an imported file which contains data item as $sheetData[$i]['A'] from an excel sheet that I need to use it to find out if BOTH tables have this item or not.
My code as follows :
$query1="SELECT * FROM table1.item,table2.item WHERE item ='".$sheetData[$i]['A']."'";
$result1= mysql_query($query1);
if(mysql_num_rows($result1)>0){
echo "This Item Found in Both Tables";
echo $sheetData[$i]['A'];
echo "<br />";
}
else{
echo "Item Could Not Be Found in both tables";
echo $sheetData[$i]['A'];
}
Its basically I want to find out if the imported item found in both tables or not. I hope this makes sense for you!
Any help would be really appreciated
Compiler can't decide which item needs to compare. item from table1 or item from table2
So write query as:
SELECT A.* , B.*
FROM table1 A, table2 B
WHERE A.item = B.item AND
A.item = '".$sheetData[$i]['A']."'
Try with -
"SELECT t1.id, t1.item, t1.qty, t2.id id_2, t2.qty qty_2 FROM table1 t1
INNER JOIN table2 t2 ON t2.item = t1.item
WHERE table1.item ='".$sheetData[$i]['A']."'"
Try to avoid using mysql, it is deprecated now. mysqli/PDO instead.
Try something like this:
SELECT COUNT(*)
FROM (
SELECT DISTINCT 1
FROM table1
WHERE item = ?
UNION ALL
SELECT DISTINCT 1
FROM table2
WHERE item = ?) AS t
This will return 2 if the item exists in both tables, 1 if it exists in only 1 table and 0 if the item doesn't exists in any of the two tables.

SQL joining other table with condition

I have this query:
SELECT *
FROM `classes`
JOIN `classes_students`
ON `classes`.`id` = `classes_students`.`class`
And I need to add condition for selecting just classes, in which are not currently logged student (user ID is not in classes_students connected with class id) and also count how many students are in that class.
Table structure:
classes: id, name, etc
classes_students: class_id, user_id, etc
Table data:
classes:
1 | test
2 | test2
3 | test3
classes_students:
1 | 1
1 | 2
2 | 3
3 | 4
3 | 5
Expected output if im user with id 1:
classes names (with number of students in):
2 (1 student)
3 (2 students)
All this in one query. It is possible? If yes, how?
Select classid, count(*)
from class
left join student on student.classid = class.classid
group by classid
Glad help for you
Try this query:
$user_id = 1; // current user_id
$query = "SELECT `classes`.`id`, `classes`.`name`, COUNT(*) as students FROM `classes`
JOIN `classes_students`
ON `classes`.`id` = `classes_students`.`class_id`
AND `classes_students`.`user_id` != $user_id
GROUP BY `classes_students`.`class_id`
";
I figured it out! :)
Thank you guys for ur help.
$user_id = 1; // current user_id
$query = "SELECT `classes`.`id`, `classes`.`name`, COUNT(*) as students FROM `classes`
LEFT JOIN `classes_students`
ON `classes`.`id` = `classes_students`.`class_id`
WHERE `classes`.`id` NOT IN (SELECT `class_id` FROM `classes_students` WHERE `user_id`='.$user_id.')
GROUP BY `classes_students`.`class_id`
";

Return all the rows even the joined table has empty results

In my table 1 I have something like this
name | age
George 42
Bob 30
Ken 23
In my table 2, I have something like this, this is where i store votes for each person.
name | votes |
George 1
Ken 1
George 1
George 1
Ken 1
My goal is to combine the 2 tables, and return all the rows in table 1 even it doesn't exist in table 2.
Desire results:
name | age | total_votes
George 42 3
Bob 30 0
Ken 23 2
But instead I get:
name | age | total_votes
George 42 3
Ken 23 2
I have tried something like this
SELECT `table_1`.*, coalesce(COUNT(`table_2`.votes), 0) AS total_votes
FROM `table_1`
LEFT JOIN `table_2`
ON `table_1`.name = `table_2`.name
You can do one of these:
1) Use Right Join instead of current Left Join.
Or
2) Exchange table1 and table2 places in your join expression, like:
FROM table_2
LEFT JOIN table_1
Try this. This works in MS Access , I think this will work on your's too just convert the query to SQL:
SELECT Table1.name, First(Table1.age) AS age, Count(Table2.Votes) AS totalVotes
FROM Table1 LEFT JOIN Table2 ON Table1.name = Table2.name
GROUP BY Table1.name;
Left Join table1 to table2 so that all entry from table1 , even if its is corresponding data is null, will be included. GROUP BY your query by name so that votes will be counted by name .

Categories