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 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
I have 2 tables and I need to add the value "user" from the table user to the table Album where user is.
Table1 (user)
Id_user auto_increase
user
Password
Table2 (album)
IdAlbum auto_increase
Title
Descripcion
user text
INSERT INTO `Table2`
SELECT (NULL,"some title","some description",`user`)
FROM `Table1` WHERE `Id_user`="12345";`
This will select the username for user 12345 from Table1 and insert it into Table2.
I have two tables. First table is je_addchoice, which contains fields like
choiceid
pollid
choicename
choicecreatorid
and the second table is je_uservote and the fields are
userid
pollid
choiceid
What i want to do is,
Display the choice names based on the no of votes in the je_uservote table
$query = select * from je_addchoice where poll_id='$poll_id' //order by (count(choiceid)) from second table
//QUERY FOR DISPLAY CHOICENAMES BASED ON COUNT OF VOTES
How to write the above query
My question is how to access the no of counts in the jeuservote table and display the choicenames based on the result count. Actually the votes for the choicenames in the addchoice table count is stored in the jeuservote table. How can i access the vote count for the choice names
SELECT *, (
SELECT count(*)
FROM je_uservote T2
WHERE T2.pollid=T1.pollID
AND T2.choiceid=T1.choiceID) AS votes
FROM je_addchoice T1
ORDER BY votes