mysql查询3表php输出名称而不是id

时间:2016-06-22 05:39:13

标签: php mysql sql

需要一些帮助。 我有3张桌子。 klients,klientwithservice,service。

table klients
id | klientrnd
---------
1  | 11231231  
2  | 22222222  

table service
id | servicename
---------
1  | Repair laptop
2  | Repair pc

table klientwithservice
id | klientrnd  | serviceid
-------------------------------
1  | 11231231  | 1
2  | 11231231  | 2
3  | 22222222  | 1
4  | 22222222  | 2

我需要输出SERVICENAME而不是ID。 我的SQL查询是:

SELECT serviceid FROM klientwithservice WHERE '$pole8' = `klientrnd`

$pole8 = klientrnd确切位于我所在页面上的人。

2 个答案:

答案 0 :(得分:1)

为此你需要加入两个表

使用以下查询

SELECT s.servicename FROM klientwithservice as kws
JOIN service as s ON s.id = kws.serviceid
WHERE  `klientrnd` = '$pole8'

答案 1 :(得分:0)

首先你的sql SELECT serviceid FROM klientwithservice WHERE '$pole8' = klientrnd是错误的。

它将返回语法错误,指出unknown column $pole8(它的确切值)是错误的

其次,你应该使用join来实现你所追求的目标。试试这个:

SELECT s.servicename FROM klientwithservice  as k
JOIN service as s ON s.id = k.serviceid
WHERE  k.klientrnd = '$pole8' 
相关问题