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

时间:2021-06-17 22:43:45

Need some help. I have got 3 tables. klients, klientwithservice, service.

需要一些帮助。我有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

I need to output SERVICENAME instead ID. My sql query is:

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

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

Where $pole8 = klientrnd exactly person on which page i placed.

$ pole8 = klientrnd恰好是我放置的页面的人。

2 个解决方案

#1


1  

for this you need to JOIN two table

为此你需要加入两个表

use below query

使用以下查询

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

#2


0  

Firstly your sql SELECT serviceid FROM klientwithservice WHERE '$pole8' = klientrnd is wrong.

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

It will return a syntax error stating the unknown column $pole8 (it's exact value) `is wrong

它将返回一个语法错误,指出未知列$ pole8(它的确切值)`是错误的

Second you should use join to achieve what you're after. Try this:

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

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

#1


1  

for this you need to JOIN two table

为此你需要加入两个表

use below query

使用以下查询

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

#2


0  

Firstly your sql SELECT serviceid FROM klientwithservice WHERE '$pole8' = klientrnd is wrong.

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

It will return a syntax error stating the unknown column $pole8 (it's exact value) `is wrong

它将返回一个语法错误,指出未知列$ pole8(它的确切值)`是错误的

Second you should use join to achieve what you're after. Try this:

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

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