I have two tables named employees and testdept.In employees table there are two fields as employee_id and department.testdept table contains two fields as epfno and deptid.
Now what I want to do is insert those id's from testdept table to employees table department field where employee_id matches with the epfno.
I have tried with the below code. It inserts the data as a new row. That is the problem.
INSERT INTO employees (department)
SELECT t.deptid
FROM testdept t, employees e
WHERE t.epfno = e.employee_id
If I understood you correctly, you want an UPDATE statement and not an INSERT :
UPDATE employees e
JOIN testdept t
ON(e.employee_id = t.epfno)
SET e.department = t.deptid
This will update the records in employee table with the data from the corresponding row in testdept table
Related
I have table employees with columns: id, employee_id with values say ABC1234 and 4 respectively.
Another table cashadvance with columns :id, date_advance, employee_id, amount with values say 3, 2018-06-20, 5, 1500.
Now i want do sql select query in PHP to show date_advance and amount for the specific user/logged in employee(username is employee_id e.g, ABC1234 plus pswd) .
I have trouble cause employee_id in cashadvance is the primary key for employee in table employees. Please help
Below is the query written for your purpose to get those data for employees in employees table and related data in cashadvance . Here an inner join is used , please run the query and let me know if you face any error .
$sql = "select *
from employees as 'emp'
inner join cashadvance as 'cad'
on cad.employee_id = emp.employee_id
where 1" ;
I have two data tables
'wp_balance' table with column Balance, and userid
Another table:
'wp_users' table with column userid and referredby
userid is common in both table where referredby is a different userid. I need to update referredby members Balance in wp_balance table where wp_balance.userid=wp_users.userid.
$sql = "UPDATE wp_balance SET balance = $sum FROM wp_balance JOIN wp_users
ON wp_balance.userid = wp_users.userid;"
Am having two tables inside my Database lets say Table1 and Table2, and am trying to copy the values from a specific rows(fields) in Table2 to a specific single row on Table1, The specific rows to be moved are determined by the User Input of Particular UserID of each row from Table1.
Table Structure:
Table 1: a_uid,a_FName,a_Username,a_PhoneNo,b_uid,b_FName,b_Username,b_PhoneNo......
And Table 2 Structure:
Table 2: uid,FName,Username,PhoneNo.....
Am uing the INSERT INTO .. SELECT statement, but with multiple WHERE clause but its giving me erros
INSERT INTO table1 WHERE uid='userinput1' (b_FName,b_Username,b_PhoneNo,) SELECT FName,Username,PhoneNo FROM table2 WHERE uid='userinput2';
But am getting Error
This type of clause was previously parsed. (near WHERE)
If the row in table1 already exists, you need an UPDATE .. JOIN statement instead of INSERT .. SELECT.
UPDATE table1 t1
JOIN table2 t2 ON t2.uid='userinput2'
SET t1.b_FName = t2.FName,
t1.b_Username = t2.Username,
t1.b_PhoneNo = t2.PhoneNo
WHERE t1.uid='userinput1'
If you don't know if the row in table1 already exists, you can use an INSERT .. SELECT .. ON DUPLICATE KEY UPDATE statement:
INSERT INTO table1 (uid, b_FName, b_Username, b_PhoneNo)
SELECT 'userinput1', FName, Username, PhoneNo
FROM table2
WHERE uid = 'userinput2'
ON DUPLICATE KEY UPDATE
SET b_FName = VALUES(FName),
b_Username = VALUES(Username),
b_PhoneNo = VALUES(PhoneNo)
Note that uid sould be primary keys or at least unique in both tables.
For sql-server it would be something like this:
INSERT INTO table1 (uid,FName,Username,PhoneNo)
SELECT #userinput1, FName,Username,PhoneNo
FROM table2
WHERE table2.uid=#userinput2;
I Need to select a specific row from a MySQLi database based off of a value in it.
For example I have a Table with a column named "house", and under that column there are maybe 5 rows with the title "house1" and three rows with the title "house2". I only want to select the rows that have "house1" in them.
This is my code
$query = "SELECT * FROM Hockey WHERE house = house1 ORDER BY attendance desc";
I then want it to make a table with only values from a row if the house is Jacksons
right now if I delete the WHERE part from my query it will make a table but it will have rows from both houses (Jacksons and Martlands)
Thanks!
$query = "SELECT * FROM Hockey WHERE house = 'house1' ORDER BY attendance desc";
I have a mysql table called jos_users_quizzes with the following columns:
id
quiz_id
user_id
I have a second table called jos_users with this columns
id
name
username
department
the user_id on first table is linked with the id of second table so
quiz_id = id (jos_users)
How can build a query to multiple insert the ids of a selected department into the jos_users_quizzes table... in one click
I am thinking meabe a sub query or a loop, but no sure how.
Thanks in advance!
INSERT jos_users_quizzes (quiz_id, user_id)
SELECT $quizID, id
FROM jos_users
WHERE department = 'selected department'
With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables. For example
INSERT INTO jos_users_quizzes (quiz_id)
SELECT jos_users.id
FROM jos_users WHERE jos_users.department = 100;