Transferring information from one table to another using PHP and MySQL

Ranveer

I have a database my_db and in it two tables t1 and t2.

t1 has two columns ID and count. ID is a list of some integers, say 1,3,4,6,7,8,9 and count is all 0s by default.

t2 also has two columns, ID2 which has a list of integers which are same as that of ID in t1. But, it is possible that they may repeat or may not be present. The second column contains some value, that isn't of much importance to this question. Clarification: ID2 can be 1,1,3,4,3,1,9,8,7,7,7.

Now, what I need to do is for every ID in t1 I need to fill in count, i.e., the number of occurrences of ID as ID2 in t2.

Running a loop through all the values in ID2 and incrementing by 1 every time in corresponding count ought to do it. But I'm unable to code it up, being new to php and sql. I can work in one table. How to work across multiple?

Chun Lin

Maybe you can try MySQL update join?

UPDATE t1 
       LEFT JOIN (SELECT id2, 
                         Count(1) AS num 
                  FROM   t2 
                  GROUP  BY id2) ref 
              ON t1.id = ref.id2 
SET    t1.count = ref.num 

Please correct me if I'm wrong.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Transferring data from one table to another using a where clause mysql

From Dev

How to get the information from table to another page using PHP and MYSQL?

From Dev

Mysql DELETE from table using information from another table

From Dev

How to select information from more then one table in MySQL Database using PHP and mySQli, has to be combined single query

From Dev

Taking ids from two rows of one MySQL table and entering those values in one row of another MySQL table using PHP

From Dev

PHP MySQL Select ID from one table and photo from another

From Dev

How to search for existence of record in one table from another using php and mysql

From Dev

mysql copying specific columns from one database table to another using php

From Dev

Copy data from one MySQL table to another when data intersect using PHP

From Dev

Insert ID from one table to another in MySQL php

From Dev

Copy only specific rows from one table to another in PHP and MySQL

From Dev

Transferring creation date from one file to another

From Dev

Transferring millions of files from one server to another

From Dev

Transferring stuff from one computer to another

From Dev

Transferring stuff from one computer to another

From Dev

transferring control from one button to another

From Dev

Transferring metadata from one EntityManager to another EntityManager

From Dev

Transferring collection object from one jsp to another

From Dev

Transferring millions of files from one server to another

From Dev

Transferring a variable from one file to another

From Dev

Transferring Integers from one class to another

From Dev

Transferring a domain from one office portal to another

From Dev

Transferring text from one form to another form

From Dev

Transferring OS from one medium to another

From Dev

Transferring data from one view to another in Rails

From Dev

Transferring multiple files from one host to another

From Dev

Add row when selected from one table to another using php

From Dev

How to submit row from one table to another using php, msqli

From Dev

Add row when selected from one table to another using php

Related Related

  1. 1

    Transferring data from one table to another using a where clause mysql

  2. 2

    How to get the information from table to another page using PHP and MYSQL?

  3. 3

    Mysql DELETE from table using information from another table

  4. 4

    How to select information from more then one table in MySQL Database using PHP and mySQli, has to be combined single query

  5. 5

    Taking ids from two rows of one MySQL table and entering those values in one row of another MySQL table using PHP

  6. 6

    PHP MySQL Select ID from one table and photo from another

  7. 7

    How to search for existence of record in one table from another using php and mysql

  8. 8

    mysql copying specific columns from one database table to another using php

  9. 9

    Copy data from one MySQL table to another when data intersect using PHP

  10. 10

    Insert ID from one table to another in MySQL php

  11. 11

    Copy only specific rows from one table to another in PHP and MySQL

  12. 12

    Transferring creation date from one file to another

  13. 13

    Transferring millions of files from one server to another

  14. 14

    Transferring stuff from one computer to another

  15. 15

    Transferring stuff from one computer to another

  16. 16

    transferring control from one button to another

  17. 17

    Transferring metadata from one EntityManager to another EntityManager

  18. 18

    Transferring collection object from one jsp to another

  19. 19

    Transferring millions of files from one server to another

  20. 20

    Transferring a variable from one file to another

  21. 21

    Transferring Integers from one class to another

  22. 22

    Transferring a domain from one office portal to another

  23. 23

    Transferring text from one form to another form

  24. 24

    Transferring OS from one medium to another

  25. 25

    Transferring data from one view to another in Rails

  26. 26

    Transferring multiple files from one host to another

  27. 27

    Add row when selected from one table to another using php

  28. 28

    How to submit row from one table to another using php, msqli

  29. 29

    Add row when selected from one table to another using php

HotTag

Archive