php mysql code to delete data from multiple table having same id - php

I have a form from where Information is entered into multiple database having same id, if by mistake wrong data is entered, I want to delete that from all tables.
How and what should I do to delete from all information related to that id from all tables.

You can do multiple table deletes:-
http://dev.mysql.com/doc/refman/5.0/en/delete.html
For example say you had 4 tables and want to delete all the records in 3 of them that relate to the 1st table:-
DELETE Table1, Table2, Table3
FROM Table0
INNER JOIN Table1
ON Table0.Id = Table1.ParentId
INNER JOIN Table2
ON Table0.Id = Table2.ParentId
INNER JOIN Table3
ON Table0.Id = Table3.ParentId
WHERE Table0.Id = 1
Of course you could also delete from the first table (Table0) as well.

Related

PHP MySQL - Show Select Option as selected If Record exist in another table using JOIN Query

I was working on a task. In which
There is a select tag in which usersnames are showing from table1. I can choose multiple options.
& there is a submit button for adding those selected options in another table2 after choosing.
Working:
There are 2 Tables: table1 & table2
Data is coming from table1 in select option
I choose multiple options and click on submit.
those list of options foreign keys will be store in table2 from table1.
But when the page show again all the options that was selected previously should be shown now.
But I am afraid from the multiple queries that should be run. like currently, i am doing in this way.
I run the following query for showing data from table1 in select option
SELECT * FROM table1
and in the loop of php i am again checking that with a query that the option of this id from table1 is exist in table2 then show selected for that option. otherwise null.
My DB Tables
table1: id, name
table2: id
so if the id from table1 exist in table2 i shown selected else not show selected.
the only issue is i want to safe myself from running multiple queries within the loop and use some JOIN for this purpose. but i have no idea that how can we use if conditions in JOIN query.
Kindly help.
Thanks.
You want to use a LEFT OUTER JOIN
SELECT *
FROM table1 AS t1
LEFT OUTER JOIN table2 AS t2
ON t1.id = t2.id
WHERE t2.id IS NOT NULL
This basically says.
Return records where the table1.id = table2.id. Ignore records that do not exist in table2 based on id

MySQL: Acquiring data from related tables

I'm looking for help with acquiring and displaying information from related tables in MySQL. I have two tables:
"table1" with columns id, name, surname
"table2" with columns id, phone
They are related by the id columns.
I'm trying to display the name, surname and phone together. What I'm currently using is:
SELECT name, surname, phone FROM table1, table2 WHERE table1.id = table2.id
However, I feel like I'm not using the relationship between the tables properly as I believe this would work between unrelated tables as well.
Also, not every id from table1 has a record in table2, meaning not everyone has a phone number. The method above results in only showing those id's that exist in both table1 and table2, while I want to display the data of those without a phone number as well, either by a blank space or a "N/A" in the phone column.
Any tips on how to properly display the data are greatly appreciated.
You need to use an LEFT OUTER JOIN statement to join the two tables together. LEFT OUTER JOIN will still show records from table1 even if there is no matching id in table2. Something like:
SELECT
table1.name,
table1.surname,
table2.phone
FROM table1
LEFT OUTER JOIN table2
ON table1.id = table2.id
More info: https://www.w3schools.com/sql/sql_join_left.asp

How to select row from two different tables using specific id in mysql?

I have 3 different tables in db those are like tbl1, tbl2, tbl3
in these all table have one same columns 'direction'.
tbl1 contain one value either tbl2 or tbl3,
tbl2 contains one value tbl3.
How can I select the rows from tbl1 and tbl2 which contains direction like equal tbl3.
i also include my two table image in this image 2 columns name is same level_redir now i want which row that have value like hardening
enter image description hereenter image description here
please suggest me which query is good for getting row from two different tables in which table columns where dir_redirect = 'hardening'.
Since your question is not clear and you have not included all the table structure its bit difficult to figure out.
Let me help you with generic way
The following query we have joined all the 3 tables based on the common tables with are present in the respective tables. The following query select all the columns from all the 3 tables.
SELECT * FROM table1 AS t1
JOIN table2 AS t2 ON t1.matching_column = t2.matching_column
JOIN table3 as t3 ON t3.matching_column = t2.matching_column
WHERE t3.column_name = 'Condition';
You can select from specific tables by the following
SELECT t1.col1, t1.col2, t2.col1, t2.col2 FROM table1 AS t1
JOIN table2 AS t2 ON t1.matching_column = t2.matching_column
JOIN table3 as t3 ON t3.matching_column = t2.matching_column
WHERE t3.column_name = 'Condition';

How to select * from multiple table

I have two tables, table1 and table2. table1 has 2 rows. table2 has 3 rows. so, totally table1 and table2 have 5 rows. I want to show the 5 rows by selecting table1 and table2 at a time. how todo? can you help me please. don't add any where clause.
depends on what the table structure is. if you have a PK > FK relationship you can join the tables like so
SELECT stuff
FROM table1 t1
JOIN table2 t2 ON t1.someID = t2.someID
if there is no correlation then you can use a UNION
SELECT stuff
FROM table1
UNION
SELECT stuff
FROM table2
One thing to note about using a UNION. the columns have to match so if you have the same type of data in both tables this works fine or else you will have to specify which columns to be selected out.

How to copy data from one table to another in MySQL?

What I'm trying to do should be really simple. I have two tables with the following columns:
Table 1:
Name, Level
Table 2:
Name, Cost
Name is the primary key. I want to combine both table's data into one table that has all three columns. What I've been trying to do is add a Cost column into Table 1 and copy all the Cost values from Table 2 into it. I've tried numerous suggestions from other threads on this site and I've never had one work for me. The new Cost column in Table 1 never budged with any new values. Why?
I am doing this on MySQL Workbench on Ubuntu.
Here's one that I tried using (New cost column already made for Tbl1):
UPDATE Tbl1
SET Cost = (
SELECT Cost
FROM Tbl2
WHERE Name = 'SpecificName')
WHERE Name = 'SpecificName;
This works when I specify individual rows but it doesn't work when I replace Name = 'SpecificName' with something like "Tbl1.Name = Tbl2.Name"
The problem you face is when some names are in one table but not the other. Here is one method, using a single create table as:
create table NameLevelCost as
select n.name, t1.level, t2.cost
from (select name from table1
union
select name from table2
) n left join
table1 t1
on t1.name = n.name left join
table2 t2
on t2.name = n.name;
This assumes that name is unique in each of the tables.
update Tbl1 t1 left join Tbl2 t2 on t2.Name = t1.Name set t1.Cost = t2.Cost;

Categories