I have these two tables in my database :
table_A: table_B:
id user grade id user date
1 Mike 9.5 1 Mike 9:05
2 Dan absent 2 Dan 7:40
3 Tom 9.4 3 Tom 3:20
4 Lina 7.4 4 Lina 7:40
5 Cynthia 5:39
6 Sam 4:50
And i'm using this SQL query, to detect which users in table_B do not exist in table_A, and select those users (not existing in table A) ids:
SELECT table_B.id FROM table_B WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A);
And i'm using this php script to put the ids in a variable $not:
$sql=" SELECT table_B.id FROM table_B WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A)";
$result = mysqli_query($con,$sql);
if (!$result) {
printf("Error: %s\n", mysqli_error($con));
exit();
}
while($row = mysqli_fetch_array($result)){
$not=$row[0];
}
The thing is, for those users not existing the query found, i want to extract their names and ids only (without date), and insert it in table_A and have an empty grade.. Any help?
Insert into table_A(user,id,grade)
SELECT table_B.id,table_B.user,'' FROM table_B
WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A);
You can use INSERT INTO SELECT.
https://dev.mysql.com/doc/refman/5.0/en/insert-select.html
Your query would look like:
INSERT INTO `table_A` (`id`, `user`)
SELECT `id`, `name` FROM `table_B`
WHERE table_B.id NOT IN (SELECT table_A.id FROM table_A);
Try this,
Insert into table_A(id, user, '') SELECT b.id, b.user FROM table_B b WHERE b.id NOT IN (SELECT a.id FROM table_A a);
Related
i am trying to achieve the following result. (i am sorry for the horrible explanation but i find it very hard to explain :P)
I need data from 2 tables.
Table1
id, table2_id, user_id
Table2
id, Name
Table1 example information
ID 1 Table2_id 1 user_id 3
ID 2 Table2_id 2 user_id 3
ID 3 Table2_id 5 user_id 3
Table2 Example Information
ID 1 Name TEST
ID 2 Name Hello
ID 3 Name Helpme
ID 4 Name Please
ID 5 Name IamLost
i Would like to output everything tied user_id 3. This would be my ideal end result
TEST
Hello
IamLost
I have this as code
$id = "3";
$sth = $db->prepare("SELECT table2_id, user_id, naam FROM table1, table2 where user_id = $id ");
$sth->execute();
$result = $sth->fetchAll();
foreach( $result as $row ) {
echo $row['table2_id'] . ' ';
echo $row['naam'] . '<br>';
}
But this just outputs everything but then twice. like this
TEST
TEST
Hello
Hello
Helpme
Helpme
Please
Please
IamLost
IamLost
A LEFT JOIN should do the trick:
SELECT `table1`.`table2_id`, `table1`.`user_id`, `table2`.`name`
FROM `table1`
LEFT JOIN `table2`
ON `table1`.`Table2_id` = `table2`.`id`
WHERE `table1`.`id` = $id
MySQL JOIN Syntax
Use Joins in SQL.
The SQL Query should look like this:
SELECT T1.USER_ID, T2.NAME
FROM TABLE1 AS T1
JOIN TABLE2 AS T2
ON T1.TABLE2_ID = T2.ID
WHERE T1.USER_ID = 3
these two table must be related to each other. When you select , returned rows should be equal this two tables. This why we use table joins e.g
SELECT a.user_id,a.table_id,b.name FROM table1 as a, table2 as b
RIGHT OUTER JOIN table 1
ON b.ID = a.table2_id
AND table1.user_id = 3
I believe you just need to be more precise:
$sth = $db->prepare("SELECT table2_id, user_id, name
FROM table1
LEFT JOIN table2
ON table1.Table2_id = table2.id
WHERE user_id = $id ");
A simple right outer join will help you here.
SELECT * FROM table2
RIGHT OUTER JOIN table 1
ON table2.ID = table1.table2_id
AND table1.user_id = 3
I want to combine between two table. The output should be data from table A, table B.
SELECT date as Date, COUNT(*) as Transaction, SUM(status=0) as Success
FROM transfer_tx_201503 WHERE time >='00:00:00' AND time <= '$searchterm'
UNION SELECT date as Date, COUNT(*) as Transaction, SUM(status=0) as Success FROM request_tx_201503 WHERE time >='00:00:00' AND time <= '$searchterm' GROUP BY date desc"
i want out put like this: |2015-03-23 | 5 | 3 | 4 | 1 |
5 and 3 from table transfer_tx_2015, 4 and 1 from table request_tx_2015
Thank you
You can use table_a, table_b syntax:
SELECT columns FROM table_a, table_b WHERE table_a.id = table_b.id
Or you can use JOIN:
SELECT columns FROM table_a JOIN table_b ON table_a.id = table_b.id
You can read more about JOIN in:
https://dev.mysql.com/doc/refman/5.5/en/join.html
SELECT columns
FROM firstTable
JOIN secondTable ON
firstTable.columnName = secondTable.columnName
The common field is the date field, hence the join should be on that field.
Try using the following SQL:
SELECT t.date as Date, COUNT(*) as Transaction, SUM(t.status=0) as Success, COUNT(*) as Request, SUM(r.status=0) as RequestSuccess
FROM transfer_tx_201503 AS t,request_tx_201503 AS r WHERE t.time >='00:00:00' AND t.time <= '$searchterm' AND t.date=r.date
SELECT coloumn FROM Table_One, Table_Two WHERE Table_One.coloumnName = Table_One.coloumnName
I have two table named table_a and table_b.
table_a data
id name
1 sahadat
2 kamrul
3 Harish
4 Ilma
5 Martin
6 Ponting
table_b data
id table_a_id
1 2
2 5
3 1
Now I want to select data from table_a like this
id data
3 Harish
4 Ilma
6 Ponting
how do I write query to get this result.
what I have tried
select * from `table_a` where `id` NOT IN (select table_a_id from table_b);
It works when table_b have some data. But when table_b is empty then it show error
select * from table_a where id not in (select table_a_id from table_b);
Please use below query.
SELECT t1.* FROM table_a AS t1 WHERE NOT EXISTS
( SELECT * FROM table_b AS t2 WHERE t2.table_a_id = t1.id);
Try it
mysql_query("SELECT * FROM table_a t1 WHERE NOT EXISTS
(SELECT * FROM table_b t2 WHERE t2.table_a_id = t1.id)");
I would like to join two tables and only print records from table 1 where the rec_number is NOT in table 2.
table 1
name rec_number
john smith 123
Bob jonson 345
etc
Table 2
Bob jonson 345
etc
What is the query in php that would do this so the query only gives me John smith, not bob jonson.
is it:
$query = "select * from table1
left join rec_number on table1.rec_number = table2.rec_number";
$result=mysql_query($query);
Thank you.
You can use this query
select
t1.*
from table1 t1
left join table2 t2
on t2.rec_number = t1.rec_number
where t2.rec_number IS NULL
Beside the left join mentioned by Abhik, you could also use a subselect:
SELECT * FROM table1 WHERE table1.name NOT IN (SELECT name FROM table2);
I have the following two tables...
Table1
colA colB colC
1 a w
2 b w
3 c s
4 b g
5 n
Table2
colA colB colC
1 w f
2 w r
3 s g
I want to copy from table1 to table2 using UPDATE query, the problem I have is I have to SET all column name in which I have 100 column for each table, But I have the same column count and name too.
what would be easy way to run query UPDATE in php?
is there some foreach thing out there might be...
Here is what I have now...
public function update($id){
try {
UPDATE table1 b
INNER JOIN connect c
ON c.ID = b.ID
INNER JOIN table2 a
ON a.ID_a = c.ID_a
SET b.colA = a.colA,
b.colB = a.colB,
b.colC = a.colC
.
.
.
.
. coontinue here all column name..
.
.
WHERE a.ID_a = '".$id."' ";
} catch(PDOException $e) {
$e->getMessage();
}
return false;
}
some idea please?
You don't need to do it via UPDATE syntax. You can use INSERT..SELECT:
INSERT INTO `table2` SELECT * FROM `table1`
this will works if you have exactly same structure in both tables. If not, you have to write column names (and map columns from first table to the second).
If you wand to replace corresponding values, then you should first delete present values:
DELETE FROM `table2` WHERE `colA` IN (SELECT `colA` FROM `table1`)
if you want to update data in first table use values in other table you can use next statement
UPDATE table_name AS t1
SET t1.field_name = (SELECT field_name FROM other_table AS t2 WHERE t1.field=t2.field)