updating sql record to two tables [duplicate] - php

This question already has answers here:
MySQL, update multiple tables with one query
(7 answers)
Closed 1 year ago.
Respected Member
my first table company has following fields
com_id
p_id
p_name
my second table users has following fields
p_id
p_name
role_def
com_id
Question
I want to update multiple record on both tables with below query as example.
Please guide as it is not being working
UPDATE company,
users
SET company.p_name='Javeria Rauf',
users.p_name='Javeria Rauf',
users.role_def='Admin'
WHERE company.p_id=9
OR users.p_id=9

Just use two separate update statement as below:
To update company:
UPDATE company SET company.p_name = 'Javeria Rauf' WHERE company.p_id = 9 ;
To update users:
UPDATE users SET users.p_name = 'Javeria Rauf', users.role_def='Admin' WHERE users.p_id = 9 ;

Related

Mysql insert query in after a chosen row? [duplicate]

This question already has answers here:
Is it possible to insert a new row at top of MySQL table?
(5 answers)
Closed 3 years ago.
I have persons in my database with their attended trainings but as time goes by, each of them will add some trainings again so i need to add it after that name.
I have table with columns id, firstname and training but that's my problem, inserting after a selected name
Sample
Insert into table (firstname, training) values ("Zarie", "Technical Writing Course") after a selected row where id = (select id where firstname="Zarie");
Thank you.
When you adding row to mysql table it goes to the end of table or on free primary key id. You can't change this when you inserting data. You can change order only when you selecting data:
SELECT * FROM table WHERE name = 'name' ORDER BY id DESC, firstname ASC
SELECT id, firstname FROM table WHERE name != 'Zimbo' ORDER BY firstname ASC

SQL Select -- Checking if value is in a comma separated list -- PHP [duplicate]

This question already has answers here:
Query with multiple values in a column
(4 answers)
MySQL search in comma list [duplicate]
(5 answers)
Closed 7 years ago.
I'm wanting to create a sql select statement that will grab rows if a given value is in a comma separated list in one of the columns of the database table.
Example Table...
id | courses
1 | 5, 8, 15, 19
I want to do something like this
$course_num = 5;
$sql = "SELECT * FROM courses WHERE $course_num IS IN courses";
1.) I don't think the "IS IN courses" part is legit. How can I do this?
2.) For my code above, I would want to return the row because of the "5" in courses, not because of the "15". So, if $course_num = 9 no rows should be returned.
Thanks for your help.
By adding comma in searched occurrence
SELECT *
FROM courses
WHERE concat(', ',course,',') like '%, 5,%'

how to move data from one table to another after some days [duplicate]

This question already has answers here:
How to store day to day records from a mysql table to another?
(2 answers)
Closed 8 years ago.
is it possible to move one table column data to another column data after 1 or 2 days i mean like we have one table which is called table1 another table is table2 table1 have some data like name contact postal address then how to move all of data from table1 selected row to table2 after a period i mean one or two and when table1 data have move to table2 it will be delete from table1 .
in more details like we insert data into table1 like entry_day column using now() we get current date is there any option to after 1 days or 2 days the data will delete from tbl1 and insert into tbl2
$q= "INSERT INTO table1 (name,contact,postal,entrydate) values ('".$row[0]."','".$row[1]."','".$row[3]."',now())";
Just create a MySQL event to handle this based on the entrydate. Its quite easy and a quick google search will turn up plenty of information on how to create them.

Finding a specific number within a comma separated list [duplicate]

This question already has answers here:
Opposite of MySQL FIND_IN_SET
(6 answers)
Closed 9 years ago.
I have a database which has a field containing some comma separated values like 1,8,3,54,5,19,9..... I want to select only those rows where 2 doesn't exists.
The query below is used for finding all fields containing the number 2 in the attachedCompanyIDs column. However, I want to find all rows where that number doesn't exist, but I don't know how to use find_in_set in this case. Can any one please help me?
SELECT name FROM company
WHERE orderID = 1 AND FIND_IN_SET(2, attachedCompanyIDs);
SELECT name FROM company
WHERE orderID = 1 AND NOT FIND_IN_SET(2, attachedCompanyIDs);
or
SELECT name FROM company
WHERE orderID = 1 AND FIND_IN_SET(2, attachedCompanyIDs) = 0;

Add column in tables [duplicate]

This question already has an answer here:
Add a new column in all mysql tables
(1 answer)
Closed 9 years ago.
I want to add a common column in all tables in a database
For example:
I want to insert the column TENANT_ID in all tables in database
How can i do that.
You use the ALTER TABLE SQL command as in:
ALTER TABLE table_name
ADD column_name datatype
If you have multiple tables you'll need to apply this to each table in succession.
More here: http://www.w3schools.com/sql/sql_alter.asp
yes you can add it
if you have list of table in array
$column_name = 'TENANT_ID';
$size = '';
foreach($all_table_name as $each_table_name) {
mysql_query("ALTER TABLE {$each_table_name} ADD {$column_name} VARBINARY({$size})");
}
above code i just skilton you have to set accourding to your code.

Categories