SQL将行内容连接到另一个表的列名

时间:2016-06-02 21:11:26

标签: mysql sql sql-server database

假设我有以下两个表:

%make array for particlesenter code here
particles={}

%define initial particle route
route={{x1,y1},{x2,y2},...}

%probably you should have your field defined somewhere here too
v_tiles={{x1,y1},{x2,y2}...}
h_tiles=...

%make particle, possible implementations are numerous really
%for example it could be represented by its position 
%and a function which gives next position, based on current one
%for main particle it could search the route table for a current position and return next element, 
%or just use external counter variable to keep track of propagation
particle={position={x,y}, propagator=function({x,y}) ....}

%put that into particles array obviously

%initialize counter
T=0
%run a cycle until there are particles
while #particles>0 do

    %perform a single step for all the particles
    for _,p in pairs(particles) do
        %move them all
        p.position=p.propagator(p.position)
    endfor
    %then check them all
    for i,p in pairs(particles) do
        %check for special tiles 
        if is_v(p.position) then
            %make new particle
            table.insert(particles,{
                position={p.position.x, p.position.y+1},
                %for secondary particles propagator would be just a constant addition to a single coordinate
                propagator=function(pos) return {pos.x,pos.y+1} end
                })
            %make second new particle

        %same for h tiles
        if is_h(p.position) then ...

        %check for border tile
        if is_border(p.position) then
            %remove those that are at the border
            table.remove...
            %for the original particle you'd have to check 
            %whether it has reached destination
            %probably you should check that somewhere around here too
    endfo
    %do what we're here for: increase counter
    T=T+1
endwhile
%when this reaches the end T will be the answer

并希望获得下表:

       **Table A**       

ID   Day   Month   Year           
------------------------       
1    1      1       1900
3    13     3       2009
49   28     2       1984


                      **Table B**     

ID   ABC_1_1_1900   ABC_2_1_1900 ...  ABC_31_12_2100           
-------------------------------- ... ---------------       
1        431            15449             98565
2                
3                       ....
.
.
n                                         ....

基本上我想要实现的是,通过将A的行内容与B的列名和ID上的内连接相匹配来获取表B的子集。

等价物是

      **Table C**     

ID        ABC           
------------------------       
1         431
3         (value B.ABC_13_3_2009 for ID=3)
49        (value B.ABC_28_2_1984 for ID=49)

遗憾的是不起作用。任何想法都非常感谢!

1 个答案:

答案 0 :(得分:0)

您要做的是从变量创建SQL语句。如何完成取决于您使用的SQL技术。从标签,我假设你使用MySQL。使用MySQL,您需要制作一个所谓的准备语句(注意:他们说它非常hacky,请参阅:mysql field name from variable)。使用预准备语句,您将能够将列内容传递给SQL语句。所有这些看起来如下:

  • 创建SQL字符串:

SET @s = CONCAT('SELECT A.ID, B.ABC_', A.Day, '_', A.Month, '_', A.Year, ' AS ABC FROM A INNER JOIN B ON A.ID=B.ID');

  • 准备声明并执行:

PREPARE stmt FROM @s; EXECUTE stmt;

注意:请注意,这可能容易出错,因为列中的数字(例如日期)必须始终格式化为与列名称(ABC_X_Y_Z)中的数字相同。