从具有类似ID的表中获取记录

时间:2015-07-22 16:24:40

标签: mysql

我有两张表,比如

   uid name
    11  Cadman
    12  Clive
    13  Coleman
    14  Chester

和第二张表

id      pa1 pa2 pa3 pa4
4800    11  12  11  14
4801    11  12  13  14
4802    11  12  12  
4973        12  13  14
6882    12  12  13  14
6883    11  12      14
6884    11  13  13  14
6885    11  13  13  14

我希望显示名称而不是ID,任何人都可以在此查询中帮助我提前感谢

2 个答案:

答案 0 :(得分:0)

SELECT t1.name 
FROM  table1 AS t1
INNER JOIN table2 AS t2 ON t1.row1 = t2.row2
INNER JOIN table2 AS t3 ON t1.row1 = t3.row3
INNER JOIN table2 AS t4 ON t1.row1 = t4.row4
INNER JOIN table2 AS t5 ON t1.row1 = t5.row5
WHERE condn;

答案 1 :(得分:0)

创建/填充

exports.search = function(lat, lng, Arr, callback1) {

//something

geocoder.reverse({
    lat: lat,
    lon: lng
}, function(err, res, callback) {

    //finding the area
    if (area !== "null") {
        pool.getConnection(function(err, connection, callback) {
            if (err) {

            } else {
                connection.query("SOME SQL CODE", function(err, rows, fields, callback) {
                    if (found what Im looking
                        for) {
                        connection.query("SOME SQL CODE", function(err, rows, fields, callback) { //looking for something else
                            if (err) {
                                callback(true);
                            } else {
                                if (rows[0] === undefined) {
                                    callback(true);
                                } else {
                                    console.log("found!");
                                    callback1(null, rows[0]);
                                }
                            }
                        });

                    } else if (err) {

                    }
                });
            }
        });
    } else {

    }
});

查询

create table nt
(   -- name table
    uid int auto_increment primary key,
    name varchar(100) not null
);

create table it
(   -- id table (sorry my creativity is at a low)
    id int primary key,
    pa1 int not null,
    pa2 int not null,
    pa3 int not null,
    pa4 int not null,
    -- in the constraining kinda mood:
    CONSTRAINT fk_pa1_nt FOREIGN KEY (pa1) REFERENCES nt(uid),
    CONSTRAINT fk_pa2_nt FOREIGN KEY (pa2) REFERENCES nt(uid),
    CONSTRAINT fk_pa3_nt FOREIGN KEY (pa3) REFERENCES nt(uid),
    CONSTRAINT fk_pa4_nt FOREIGN KEY (pa4) REFERENCES nt(uid)
);

insert nt (name) values ('kurt'),('burt'),('curt'),('dirt'),('lert'),('murte'),('qbert');
insert nt (name) values ('jim'),('kim'),('dim'),('sim'),('tim'),('Cadman'),('bim');

insert it(id,pa1,pa2,pa3,pa4) values (4800,11,12,13,14);
insert it(id,pa1,pa2,pa3,pa4) values (4801,3,5,7,9);