在SELECT(MYSQL / PHP)中连接2个表

时间:2011-11-11 13:02:50

标签: php mysql select

我有这两张桌子。连接“密钥”是conID,它包含在两个表中。 所以现在我想写一些select语句,它会给我这样的东西:

John Smith dairy22 Texas 4000 smth4

迈克情况glenn32球衣1000 smth1 ...

>表“人”:

NUM   Name lastName  address    conID
-----------------------------------------
  1    John  Smith     dairy22   Texas
  2    Mike  Situation glenn32   Jersey
  3    Duke  Nukem     haris48   NYork
  4    Queen Lisa      court84   London

>表“国家”

conID   postNum   region
-------------------------
Jersey  1000      smth1
NYork   2000      smth2
London  3000      smth3
Texas   4000      smth4

! - > NUM是AUTO INCREMENT主键,如果可能,我不希望输出。

提前感谢您的帮助:))

6 个答案:

答案 0 :(得分:2)

这应该根据num列返回您想要的行:

SELECT Name, lastName, address, people.conID, postNum, region 
FROM people 
JOIN countries 
ON people.conID = countries.conID 
WHERE num=1

答案 1 :(得分:1)

您可以使用以下MySQL查询:

SELECT
    Name,
    lastName,
    address,
    people.conID,
    postNum,
    region
FROM
    people
JOIN
    countries
        ON people.conID = countries.conID
WHERE
    num = 1

答案 2 :(得分:0)

这可能会起到作用:

SELECT name, lastName, address, people.conID, postNum, region
FROM people
 JOIN countries ON people.conID = countries.conID;

答案 3 :(得分:0)

您的SQL查询应如下所示:

SELECT p.Name, p.lastName, p.address, p.conID, c.postNum, c.region
FROM people p 
LEFT JOIN countries c ON p.conID = c.conID

答案 4 :(得分:0)

Good ole USING条款:

SELECT Name, lastName, address, p.conID, postNum, region FROM people p
INNER JOIN countries USING (conID);

答案 5 :(得分:0)

SELECT Name, lastName, address, people.conID, postnum, region
FROM people JOIN countries ON people.conID = countries.conID;