How to compare 2 columns of 2 different tables on Mysql - php

I did something like this with PHP and it works.
$table1 = $DB->query('SELECT column1 FROM table1');
$table2 = $DB->query('SELECT column2 FROM table2'); # excludes
$diff = "'" . implode("','", array_diff($table1, $table2)) . "'";
$DB->query("DELETE FROM table1 WHERE column1 IN ({$diff})");
My question is how to do the same with one sql statement, instead of writing 3 different and doing the array_diff/implode thing.
Unfortunately I have limited sql knowledge but I'm pretty sure it's possible, I think with JOIN but after 2 hours still can't find how. :/

You can use sub-query. The query will be like below.
Delete from Table1
where column1 in (
SELECT column2 FROM table2
)

You can try this:
DELETE t1
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.column1 = t2.column2;

Here is a solution
DELETE FROM table1 WHERE column1 IN (
SELECT t0.column1 FROM
(SELECT column1 FROM table1) AS t0
JOIN
(SELECT column2 FROM table2) AS t1
ON t0.column1 = t1.column2)
Thanks.

Related

SQL Select from table using the outcome of a previous SELECT query?

Basically, I want to Select from one table, then using the outcome select from another table and then finally echo the values I selected.
DECLARE #var1 int; /*This should become the result of Table1.Column1 */
DECLARE #var2 nchar(50); /*This should become the result of Table1.Column2 */
DECLARE #var3 nchar(50); /*This should become the final result */
SELECT #var1 = (SELECT Column1 FROM Table1 WHERE Column3 = '12345')
SELECT #var2 = (SELECT Column2 FROM Table1 WHERE Column3 = '12345')
SELECT Column1 FROM Table2 WHERE Id = #var1
Then once this has finished, PHP echo var1, var2 and var3.
I know this would be easier with 2 seperate queries but I want to keep it clean and not have to do that, does anyone know how I can do this? I know the code I provided is completely off but hopefully it makes sense what I'm trying to do.
This is what a join is for. Your first two example SELECT statements can be combined into a single statement like:
SELECT column1, column2 FROM table WHERE column3 = '12345';
You can then JOIN your table2:
SELECT
t1.column1,
t1.column2,
t2.column1
FROM Table1 T1
LEFT OUTER JOIN Table2 T2
ON t1.column2 = t2.id
WHERE t1.Column3 = '12345';
Using a LEFT OUTER JOIN here since that is the type of join that foots best with your psuedo code above. Essentially "SELECT all records that match the WHERE criteria from table1 and any records that might also match from table2 based on that ON condition. If no records match in Table2 for that ON Condition then just return NULL for that table2 column".
The result set returned will have three fields which are functionally equal to your #var1, #var2 and #var3
You can do this in two ways. Since it seems that your first query returns a single result, you can use that subselect as the expression in your second query:
SELECT #var2 = (SELECT Column2 FROM Table1 WHERE Column3 = (SELECT Column1 FROM Table1 WHERE Column3 = '12345'))
or you could use a common table expression (I'm assuming SQL Server):
with CTE_Table (var1) as
(SELECT Column1 FROM Table1 WHERE Column3 = '12345')
SELECT #var2 = (SELECT Column2 FROM Table1 WHERE Column3 = (select var1 from CTE_Table))
Something of that circumference should work. I just entered that here for context, but have not tested for syntax.

Php how to use multi tables in one mysql query

So I have two tables in my phpmyadmin like
tabel1 and tabel2
in both tables, i want to select id = 2
so I have tried
mysql_query('SELECT * FROM tabel1, table2
WHERE id=2');
but not working plz give me some suggestions
You can use UNION ALL to accomplish what you want.
SELECT * FROM table1 WHERE id = 1
UNION ALL
SELECT * FROM table2 WHERE id = 1
Depending on your exact requirements, the query might be as easy as
SELECT *
FROM table1, table2
WHERE table1.id=2 AND table2.id=2
You are implicitly joining your tables for the condition of table1.id and table2.id being equal 2.
Use inner join and also read Manual
mysql_query('SELECT * FROM tabel1 as t1
inner join table2 as t2 on t1.id=t2.id
WHERE t1.id=2');
I think you have a typo there. You said you have table1and table2
Then your SQL statement should be
mysql_query('SELECT * FROM table1, table2 WHERE id=2');
instead of
mysql_query('SELECT * FROM tabel1, table2 WHERE id=2');
what was in your question.

PHP SQL get data from 2 tables and output wanted information

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

Assigning the value to a column from a sql query

I have 2 tables and what I need is to selelct some of the columns from table1 and copy those columns in the table2. I have tried many options but nothing is working for me. I am a beginner in the php and mysql field . These are the things that I have tried first.
`$sql = " UPDATE table1 JOIN table2 ON table1.primary_key =
table2.primary_key SET table1.column1= table2.column1 , table1.column2=
table2.column2 WHERE table1.primary_key = 1 ";
and
`$sql= "update table1 set table1.column1 =
table2.column1 FROM table1 INNER JOIN
table2 on table1.primary_key =
table2.primary_key WHERE table2.primary_key= 1 ";
and because of the reason that bothof them are not working, I tried another method:
$sql= "SELECT column1,column2FROM table2 WHERE primary_key=1";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);`
$UpdateQuery ="UPDATE table1 SET column1=$row[column1] ,column2=$row[column2] WHERE primary_key= 1";
mysql_query($UpdateQuery);
This is also throwing an error . Can any one tel me how to assign the column value from sql query or ny other methods that will satisfy my need .
One option is to use a JOIN inside your SQL query during the UPDATE:
UPDATE table1 t1
JOIN
(
SELECT * FROM table2
WHERE table2.primary_key = 1
) t2
ON t1.primary_key = t2.primary_key
SET t1.column1 = t2.column1
I will leave it to you to insert this into your PHP code since so many details are not present in the original problem.
When you want to insert data from a different table you can use insert... select statement available with mysql.
Try like this:
INSERT INTO table1 (table1.column1,table1.column2)
SELECT table2.column1,table2.column from table2
WHERE table1.primary_key = 1
ON DUPLICATE KEY UPDATE table1.column1= table2.column1 , table1.column2=
table2.column2
For more information you can refer: http://dev.mysql.com/doc/refman/5.6/en/insert-on-duplicate.html

Fetch data from multiple MySQL tables

My two tables look like this:
TABLE1 TABLE2
+--------------------+ +--------------------+
|field1|field2|field3| and |field2|field4|field5|
+--------------------+ +--------------------+
I am already running a SELECT query for TABLE1, and assorting all of the data into variables:
$query = "SELECT * FROM TABLE1 WHERE field2 = 2";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
if((!is_bool($result) || $result) && $num_rows) {
while($row = mysql_fetch_array($result))
{
$field1 = $row['field1'];
$field2 = $row['field2'];
$field3 = $row['field3'];
}
}
What I want to do is get the data from 'field4' on TABLE2 and add it to my variables. I would want to get field4 WHERE field2 = 2
SELECT
t1.*,
t2.field4
FROM
TABLE1 AS t1,
TABLE2 AS t2
WHERE
t1.field2 = 2
AND
t1.field2 = t2.field2
You want to join the two table which can be done explicitly (using the JOIN operator or one of its variants) our implicitly (by SELECTing from multiple tables directly).
From your description, I suppose that you want to do the join where field2 in the two tables have the same value (2).
If TABLE2 will not always provide a valid join candidate but you still want the data from TABLE1, you should use a LEFT JOIN (giving NULL for field4 where nothing matched):
SELECT
t1.*,
t2.field4
FROM
TABLE1 AS t1
LEFT JOIN
TABLE2 AS t2
ON
t1.field2 = t2.field2
WHERE
t1.field2 = 2
http://en.wikipedia.org/wiki/Join_%28SQL%29
You need to use a JOIN:
SELECT TABLE1.*,TABLE2.field4 FROM TABLE1 JOIN TABLE2 ON TABLE1.field2=TABLE2.field2 WHERE TABLE1.field2=2;
hi try the following as ur query.
It is not preferred all the time to use * in the select query
SELECT T1.field1,T1.field2,T1.field3, T2.field4 FROM TABLE1 AS T1 INNER JOIN TABLE2 AS T2 ON T1.field2=T2.field2 WHERE field2 = 2

Categories