Mysql select from a table and insert into another - php

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;

Related

WordPress update statement based on join 2 tables in MySQL

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;"

Using Multiple WHERE clause in an INSERT INTO ... SELECT statement

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;

Mysql Insert with the Select

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

MYSQL insert data from one table to another?

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.

select datas from firsttable and order by field in secondtable in mysql

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

Categories