Is it possible to reference a mysql table entry value from a second table entry dynamically?

ぐ巨炮叔叔 提交于 2019-12-12 04:54:11

问题


I can't find anything about dynamically referencing one MySQL table entry to another. It may not be possible.

Essentially, I'd like to know if in MySQL you can do the equivalent to referencing the value of a certain Excel cell to another. For example, if in Excel I set Sheet 1 Cell A1 to some value like "MyVal". Then if I set Sheet 2 Cell A1 to "=Sheet1!A1" and Sheet 3 Cell A1 to "=Sheet2!A1" the value of Sheet 3 Cell A1 is "MyVal". If I go back to Sheet 1 Cell A1 and change the value to "MyNewVal" then the value is automatically updated on Sheet 2 Cell A1 and Sheet 3 Cell A1 to "MyNewVal".

My question is... in MySQL can I set the value of a certain entry in the first table to be dynamically linked to the value of a different entry in a second table such that when I query the first table (using existing PHP code) I get the value that's in the second table? I imagine that if it's possible then perhaps the value of the entry in the first table would look like a query that queries the second table for the correct value.

I understand how to write an UPDATE query in PHP to explicitly make the values the same but I don't want to change the existing php code. I want to link them in a relative/dynamic way. The short reason is that I don't want to change the PHP code since the same code is used on several of the sites I maintain and I want to keep the existing php code the same for cleaner maintenance/upgrading.

However, since the databases on the various sites are already different, it would be very clean to somehow dynamically link the appropriate entries in the different tables in the database itself.

Any help would be very appreciated. If this is possible, if you could just point me in the right direction, I'd be happy to do the research.


回答1:


There are 2.5 ways to do this (basically two, but it feels like there's three):

From easiest to hardest...

Option 1:

If you need tableA to reflect tableB's value, don't store the value in tableA at all, just use tableB's value. Use either a join:

select a.*, b.col1
from tableA a
join tableB b on <some join condition>

or a subselect

select *, (select col1 from tableB where <some condition>) col1
from tableA

Option 2:

If you're happy with option 1, convert it to a view, which behaves like a table (except are restrictions on updating views that are joins):

create view myview as 
select ... (one of the above selects)

Option 3:

Create a database trigger that fires when tableB's value is changed and copies the value over to the appropriate row/column in tableA

create trigger tableB_update
after update on tableB
for each row
update tableA set
tablea_col = new.col1
where id = new.tableA_id;

Note that new and old are special names given to the new and old rows so you can reference the values in the table being updated.

Choose the option that best suits your needs.




回答2:


Databases don't really provide this type of facility, it's a completely different paradigm.

You can achieve the same results using joins, groupings or functions.

ALternatively if you wish to save the representation, store the query into a view which makes it more re-usable from various interfaces. More information on views can be found here; http://www.mysqltutorial.org/mysql-views-tutorial.aspx

Anything more complex and you will need to look at some business analysis tools.




回答3:


Perhaps you have oversimplified the question, but you should not need to use a trigger. Just join the tables and any time 'MyVal' is changed it will automatically be available through query.

CREATE TABLE Sheet1
    (
      `ID` int auto_increment primary key
    , `A` varchar(5)
    )
;

INSERT INTO Sheet1
    (`A`)
VALUES
    ('MyVal')
;

CREATE TABLE Sheet2
    (
      `ID` int auto_increment primary key
    , `Sheet1FK` int)
;

INSERT INTO Sheet2
    (`Sheet1FK`)
VALUES
    (1)
;

CREATE TABLE Sheet3
    (
      `ID` int auto_increment primary key
    , `Sheet2FK` int)
;

INSERT INTO Sheet3
    (`Sheet2FK`)
VALUES
    (1)
;

Query 1:

select
        sheet3.id id3
      , sheet2.id id2
      , sheet1.id id1
      , sheet1.a
from sheet1
inner join sheet2 on sheet1.id = sheet2.sheet1fk
inner join sheet3 on sheet2.id = sheet3.sheet2fk

Results:

| ID3 | ID2 | ID1 |     A |
|-----|-----|-----|-------|
|   1 |   1 |   1 | MyVal |


来源:https://stackoverflow.com/questions/26541005/is-it-possible-to-reference-a-mysql-table-entry-value-from-a-second-table-entry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!