I have here two tables, I need to update both table row field from a post value. table1.item and table2.item update from post value. I didn't know how to set both table field equal to post value.
Any help will appreciate.
Table1
| pr_id | item |
----------------
| 1001 | pen |
Table2
| pr_id | item |
----------------
| 1001 | pen |
Query
<?php
$pr = $mysqli->real_escape_string($_POST["pr_id"]);
$item= $mysqli->real_escape_string($_POST["item"]);
$mysqli->query("UPDATE table1 LEFT JOIN table2 ON table1.pr_id = table2.pr_id WHERE table1 .pr_id = '$pr' ");
?>
Try this:
update table1 inner join table2
on table1.pr_id=table2.pr_id
//your changes
set table1.item ='some thing', table2.item ='some thing'
where table2 .pr_id ='$pr' ;
Try this:
Query:
UPDATE table1 T1 LEFT JOIN
table2 T2 ON T1.pr_id = T2.pr_id
SET T1.Item= '$item', T2.Item = '$item'
WHERE T1.pr_id = '$pr'
With php:
$mysqli->query("UPDATE table1 T1 LEFT JOIN table2 T2 ON T1.pr_id = T2.pr_id SET T1.Item= '$item', T2.Item = '$item' WHERE T1.pr_id = '$pr' ");
According to my opinion it is better to use Compound Trigger for updating 2 diffenent tables at the same time. Try to use that.
Related
I have two tables say named table_1 and table_2. The schema is somewhat as follows.
table_1
+----+--------+-----+------------+
| id | reg_no | ... | table_2_id |
+----+--------+-----+------------+
table_2
+----+-----+
| id | ... |
+----+-----+
The column table_2_id in table_1 refers to the column id in table_2. Now, I have to get the table_2_id for a specific reg_no and then use that table_2_id to get data from table_2.
I currently do it as follows and it works.
$stmt = $this->conn->prepare("SELECT table_2_id from table_1 WHERE reg_no = ?");
$stmt->bind_param("s", $reg_no);
$stmt->execute();
$stmt->bind_result($table_2_id);
$stmt->fetch();
$stmt->close();
$stmt = $this->conn->prepare("SELECT * from table_1 WHERE id = ?");
$stmt->bind_param("i", $table_2_id);
$stmt->execute();
...
Is this the correct way to do it? Is there some other more efficient query to perform this task?
The query using join
SELECT t2.*
FROM table_1 t1
JOIN table_2 t2 ON t1.table_2_id = t2.id
WHERE t2.t1reg_no = ?
It should be more efficient to do this with one query like this:
SELECT table_2.*
FROM table_1 LEFT JOIN table_2 ON table_1.table_2_id=table_2.id
WHERE table_1.reg_no = ?
table_1 LEFT JOIN table_2 ON table_1.table_2_id=table_2.id will join table_1 and table_2.
More specifically it will create a table which will have all columns from both table_1 and table_2
and then it will put in it all rows from table_1. Also when there is a row in table_2 with id same as the
table_2_id it will also fill the tables_2's columns.
The above action will be limited to only those rows where table_1.reg_no = ?
Finally with SELECT table_2.* we get only the columns from the table_2 as in your example.
Try
SELECT T2.* FROM table_2 T2
LEFT JOIN table_1 T1 ON T1.table_2_id = T2.id
WHERE T1.reg_no = ?;
Example
table_1
id reg_no table_2_id
1 001 1
2 002 2
3 003 4
4 004 7
5 005 8
table_2
id column_test
1 A
2 B
3 C
4 D
5 E
6 F
7 G
Query
SELECT T2.* FROM table_2 T2
LEFT JOIN table_1 T1 ON T1.table_2_id = T2.id
WHERE T1.reg_no = 003;
Output
id column_test
4 D
Hello! I need help i have table that has this value
Table1
sorter | valuedata | status | useraccounts |
----------------------------------------------
same | value1 | Disabled | user1 |
same | value2 | Active | user1 |
And
Table2
name | useraccounts |
-------------------------
user1 | displayname1 |
and I wanted to display all the data even they have different status while they are innerjoin like this
SELECT table1.name,table2.useraccounts
FROM table1 INNER JOIN table2 ON table1.useraccounts = table2.name
WHERE table1.sorter = "same" AND table1.status = "Disabled" AND table1.status != "Disabled"
is this even possible to do??
I think you made 2 mistakes:
table 1 does not have name column
what you are look for is an OR instead of AND.
The next is what I think you are looking for:
SELECT table2.[name],table2.useraccounts
FROM table1 INNER JOIN table2 ON table1.useraccounts = table2.name
WHERE table1.sorter = 'same' AND
(table1.status = 'Disabled' OR table1.status != 'Disabled')
and this can be rewritten to the next since checking for 'Disabled' or != 'Disabled' is ALL ROWS.
SELECT table2.[name],table2.useraccounts
FROM table1 INNER JOIN table2 ON table1.useraccounts = table2.name
WHERE table1.sorter = 'same'
Actually, you need the cross join. You can write it without using the JOIN as well:
SELECT table2.name,table2.useraccounts
FROM table1,table2 WHERE table1.useraccounts = table2.name
AND table1.sorter = "same"
SELECT table2.name,table1.useraccounts
FROM table1 INNER JOIN table2 ON table1.useraccounts = table2.name
U will get all data while status is differnt or not
Table_1
ID | Username
1 John
2 Mike
3 Chase
4 Shane
Table_2
ID | Username
1 | John
2 | Kenny
3 | Chase
4 | Shane
I want to get ID from Table_1. Then find that ID in table_2. Then i need it to look at the field Username in both tables. and if they match do nothing, If they don't match then update it to the username in Table_2 and run some code (like email me)
I need to to check every row in Table_1 everytime I run the script.
You have to use INNER JOIN in case to find the matching ID in both the tables and if you got one then update.
$sql = mysql_query("SELECT table_1.id as id_one,table_1.name as name_one,table_2.name as name_two FROM table_1 INNER JOIN table_2 ON (table_1.id = table_2.id)") or die(mysql_error());
if(mysql_num_rows($sql) > 0)
{
while($fetch = mysql_fetch_assoc($sql))
{
if($fetch['name_one'] != $fetch['name_two'])
{
// UPDATE table_1 'name' FIELD
mysql_query("UPDATE table_1 SET name = '".$fetch['name_two']."' WHERE id = ".$fetch['id_one']) or die(mysql_error());
// DO WHATEVER YOU WANT
}
}
}
My MySQL is a little rusty, but it can be done with a single Update statement. The following should update the usernames in Table_1 only where they differ from Table_2. You may have to tweak the syntax a little...
update Table_1 t1
join Table_2 t2
on t2.id = t1.id
and t2.Username <> t1.Username
set t1.Username = t2.Username;
You can query beforehand to generate any email you need.
I have multiple tables in my database. Let's say the table users looks like this:
Users:
|id|name|gender|access|id_ext|
|1 | a | m | 1 | 32 |
|3 | b | m | 3 | 33 |
|4 | c | m | 1 | 34 |
|5 | d | f | 1 | 35 |
I would like to select the user with for example id_ext = 32 and then run another select statement using that selected users fields.
I can solve this by first getting the user with a query and then create another query with users info, but there must be a way to do this in the same query?
This is the query i use now:
SELECT * FROM users NATURAL JOIN
(SELECT id FROM ages WHERE age BETWEEN
(SELECT limit_age_l FROM users WHERE id=17)
AND (SELECT limit_age_h FROM users WHERE id=17)) as a
WHERE NOT id = 17
AND locale = 'en_US'
AND limit_gender = 1
AND visible = 0
AND NOT EXISTS (SELECT view_id FROM matches WHERE user_id = 17 AND view_id = a.id)
LIMIT 1
Problem is that the values id=17, limit_gender=1 and locale = 'en_US' in the query are not known. These are taken from the user with id_ext = '32'.
SELECT * FROM Users WHERE id in (SELECT id FROM Users WHERE id_ext='32');
Yes - assuming your subsequnt query is of the form:
select field1, field2, ...
from Table1
join Table2 on ...
where ...
and Table1.id = N /* previously selected id from users */
Then either by using the first query as a subquery:
select field1, field2, ...
from Table1
join Table2 on ...
where ...
and Table1.id = (select id from users where id_ext ='32')
/* replace = with IN if more than one id will be returned */
Or by joining to the results of the first query as part of the subsequent query:
select field1, field2, ...
from users
join Table1 on Table1.id = users.id
join Table2 on ...
where ...
and users.id_ext ='32'
(Note that both of these forms assume that users is not already being joined in the existing query - if it is, just add the users.id_ext ='32' condition to the existing query.)
EDIT: If I have understood the requirements correctly, the required query could be written as:
SELECT u.*
FROM users u
join ages a on u.id = a.id and
u.age between limit_age_l and limit_age_h
join users ul on ul.id = 17 and
ul.id <> u.id and
ul.locale = u.locale and
ul.limit_gender = u.limit_gender and
ul.visible = u.visible
AND NOT EXISTS (SELECT NULL
FROM matches m
WHERE m.user_id = ul.user_id AND m.view_id = a.id)
LIMIT 1
SELECT * FROM users WHERE id = (SELECT id FROM users WHERE id_ext = '32');
Select * from users as user inner join userinfo as usinfo on usinfo.id=user.id_ext where user.id_ext='32'
table1 (ids always exist)
+----+------+
| id | col1 |
+----+------+
| 1 | ab |
+----+------+
| 2 | gh |
+----+------+
table2 (ids always exist)
+----+------+
| id | col2 |
+----+------+
| 1 | cd |
+----+------+
| 2 | ij |
+----+------+
table3 (ids might be missing, in this case 2 is missing)
+----+------+
| id | col3 |
+----+------+
| 1 | ef |
+----+------+
PHP
$col = 'ab';
$a = mysql_query("SELECT t1.id FROM table1 AS t1, table2 AS t2, table3 AS t3
WHERE t1.id = t2.id AND t2.id = t3.id AND (t1.col1 = '$col' OR t2.col2 = '$col'
OR t3.col3 = '$col) GROUP BY t1.id, t2.id, t3.id");
That would only work if all three tables had "the same id" included, but what happens if an "id" is missing in table3 for whatever reason? how can I still test for all three tables and get t1.id to output 1, when $col = ab? would I have to use left join?
$a = mysql_query("SELECT t1.id FROM table1 AS t1, table2 AS t2
LEFT JOIN (SELECT id FROM table3 WHERE col3 = '$col') AS t3 ON t3.id = t1.id
WHERE t1.id = t2.id AND (t1.col1 = '$col' OR t2.col2 = '$col')
GROUP BY t1.id, t2.id");
what am I doing wrong here?
What are you doing wrong? Querying a table that doesn't exist. That's always going to raise an error.
I'm not going to address the wisdom of designing a database in which tables crucial to your queries come and go.
Your only hope on the client side is to
test for the existence of the tables
you're interested in, and
execute different SQL statements
based on those results.
[After your edit]
It sounds like you need either one or two left outer joins. This gives you all the ids that are common to both table1 and table2, regardless of whether they're in table3.
select t1.id, t2.id, t3.id
from table1 t1
inner join table2 t2 on (t1.id = t2.id)
left join table3 t3 on (t1.id = t3.id);
And this gives you all the ids that are in table1, regardless of whether they're in table2 or table 3.
select t1.id, t2.id, t3.id
from table1 t1
left join table2 t2 on (t1.id = t2.id)
left join table3 t3 on (t1.id = t3.id);
And, of course, you can filter the results with your WHERE clause.
LEFT JOIN only works when the table DOES exist, but contains no data.
If you don't want to have to do stuff like Catcall suggested (check if the table exists, and use different SQL statements based on that...) you have to make sure that the table exists in the database, even if it's completely empty.
Under normal circumstances, you should have a certain degree of control over your application's database, so you should be able to make sure that the table is really there.
If this is really a big problem (like, that you can't be sure if someone deleted the table) you could check this every time you start your application: create the table if it doesn't exist.