Update value in column based on column count in another table with mysql

NoCode

As I couldn't find anything like this anywhere I'm guessing it's unlikely that it is possible. Just to be sure I figured I'd ask it here.

I have a database with 2 tables. Lets name them table A and B for now. I have a bunch of items in table B, of which each item has a many to one relation to an item in table A. Now in table A I have a field which should contain the count of how many items in table B relate to that particular item in A. Example table A:

+----+------+---------+
| id | data | count_b |
+----+------+---------+
|  1 | blah |      2  |
|  2 | blah |      0  |
|  3 | blah |      1  |
+----+------+---------+

Example table B:

+----+------+--------+
| id | data | parent |
+----+------+--------+
|  1 | blah |      1 |
|  2 | blah |      1 |
|  3 | blah |      3 |
+----+------+--------+

Now I am wondering if there is a way to automatically update the column count_b in table A everytime a row is inserted in to B?

I'd like to achieve this using mysql only. My web application uses doctrine for database mappings, so if I could somehow define it as a relation there, it would be fine as well.

I tried explaining my question as clear as possible, if it's unclear just le me know in the comments. Thanks in advance.

wvdz

To do exactly what you want, you can create a TRIGGER. To achieve a somewhat similar result, you could make a view on this table that contains an extra column containing this count.

I have a strong preference for the second option, because it prevents inserting redundant data into your database and also eliminates the need for a trigger, which is good if you realize that triggers can quickly turn into a performance and maintenance nightmare.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Update value in a column based on another column in the same table in MYSQL

From Dev

Update column in one table based on value in another table in mysql

From Dev

Update column in one table based on value in another table in mysql

From Dev

Column loop and update another column with COUNT() value from another table

From Dev

mysql update column value from another table

From Dev

Mysql - update table column from another column based on order

From Dev

Update a column in a table based on column in another table

From Dev

count one column and update another in same table in mysql

From Dev

How to update mysql column with another value from another table?

From Dev

How to update mysql column with another value from another table?

From Dev

Update column from another table column value

From Dev

Mysql update all rows value with count of same table column

From Dev

MySQL update column which is a value in another column

From Dev

MySQL update column which is a value in another column

From Dev

Update MySQL table taking the value of a column, adding it to another column, the changing the value of the first column

From Dev

How to update table based on another table column?

From Dev

UPDATE sql column with value from another column based on a date column

From Dev

MySQL query to sum one column and count another column, from two tables, based on a common value?

From Dev

Group items in a table based on value in column and count

From Dev

mysql update based on time stamp in another column

From Dev

SQL add count column based on same table and grouped by another column

From Dev

Update newly added column with value on another table

From Dev

mySQL update column with concat of another table

From Dev

mysql update an column from another table

From Dev

MySQL update column from another table

From Dev

pandas increment count column based on value of another column

From Dev

Retrieving count of column values based on another column value

From Dev

MySQL sum a column based on another distinct column count

From Dev

MySQL sum a column based on another distinct column count

Related Related

  1. 1

    Update value in a column based on another column in the same table in MYSQL

  2. 2

    Update column in one table based on value in another table in mysql

  3. 3

    Update column in one table based on value in another table in mysql

  4. 4

    Column loop and update another column with COUNT() value from another table

  5. 5

    mysql update column value from another table

  6. 6

    Mysql - update table column from another column based on order

  7. 7

    Update a column in a table based on column in another table

  8. 8

    count one column and update another in same table in mysql

  9. 9

    How to update mysql column with another value from another table?

  10. 10

    How to update mysql column with another value from another table?

  11. 11

    Update column from another table column value

  12. 12

    Mysql update all rows value with count of same table column

  13. 13

    MySQL update column which is a value in another column

  14. 14

    MySQL update column which is a value in another column

  15. 15

    Update MySQL table taking the value of a column, adding it to another column, the changing the value of the first column

  16. 16

    How to update table based on another table column?

  17. 17

    UPDATE sql column with value from another column based on a date column

  18. 18

    MySQL query to sum one column and count another column, from two tables, based on a common value?

  19. 19

    Group items in a table based on value in column and count

  20. 20

    mysql update based on time stamp in another column

  21. 21

    SQL add count column based on same table and grouped by another column

  22. 22

    Update newly added column with value on another table

  23. 23

    mySQL update column with concat of another table

  24. 24

    mysql update an column from another table

  25. 25

    MySQL update column from another table

  26. 26

    pandas increment count column based on value of another column

  27. 27

    Retrieving count of column values based on another column value

  28. 28

    MySQL sum a column based on another distinct column count

  29. 29

    MySQL sum a column based on another distinct column count

HotTag

Archive