使用内部联接获取单个值

时间:2014-09-23 06:09:16

标签: php mysql database inner-join

我有两个表calldetails和calltype

calldetails表

id calltype isactive
1   1       1
2   1       1
3   1       1

Calltype表

id name             isactive
1  mobile internet   1

预期结果

id name             isactive
1  mobile internet   1

但是我得到了结果

id name             isactive
1  mobile internet   1
1  mobile internet   1
1  mobile internet   1

我使用了以下查询

SELECT ct.`name` , ct.`id`
FROM calltype AS ct
INNER JOIN calldetails AS cd ON cd.`isactive` =1
WHERE ct.`isactive` =1
AND ct.`id` = cd.`calltype`
LIMIT 0 , 30

如何使用内部联接获取单个值。

3 个答案:

答案 0 :(得分:1)

尝试使用DISTINCT

SELECT DISTINCT ct.`name` , ct.`id`
FROM calltype AS ct
INNER JOIN calldetails AS cd ON cd.`isactive` =1
WHERE ct.`isactive` =1
AND ct.`id` = cd.`calltype`
LIMIT 0 , 30

答案 1 :(得分:1)

在calldetails的选择中使用distinct:

SELECT ct.`name` , ct.`id`
FROM calltype AS ct
INNER JOIN (SELECT DISTINCT calltype, isactive FROM calldetails) cd ON cd.`isactive` =1
WHERE ct.`isactive` =1
AND ct.`id` = cd.`calltype`
LIMIT 0 , 30

答案 2 :(得分:0)

尝试

SELECT ct.`name` , ct.`id`
FROM calltype AS ct
INNER JOIN calldetails AS cd ON cd.`isactive` = ct.`isactive`
WHERE ct.`isactive` =1
LIMIT 0 , 30

这是检查内部联接命令的链接: http://www.w3schools.com/sql/sql_join_inner.asp