Home > Backend Development > PHP Tutorial > 这样的MYSQL语句该怎么写?

这样的MYSQL语句该怎么写?

WBOY
Release: 2016-06-06 20:51:57
Original
1119 people have browsed it

搜索point的和,条件是从名字等于param值的数据表中,id等于param_id值的uid等于3

SELECT sum(point) FROM fir WHERE (SELECT uid FROM fir.param WHERE id = fir.param_id) = '3'

以上语句fir.param其实是数据库fir的一个字段的值,它存储的是第二个或者第三个数据库的名字(比如sec),但是这么写报错了。请问我该怎么写?

fir
param  |  param_id   |   point
sec    |  11         |   10
third  |  23         |   -20

sec    |   
id     |   uid
11     |   3

third  |   
id     |   uid
23     |   3
Copy after login
Copy after login

回复内容:

搜索point的和,条件是从名字等于param值的数据表中,id等于param_id值的uid等于3

SELECT sum(point) FROM fir WHERE (SELECT uid FROM fir.param WHERE id = fir.param_id) = '3'

以上语句fir.param其实是数据库fir的一个字段的值,它存储的是第二个或者第三个数据库的名字(比如sec),但是这么写报错了。请问我该怎么写?

fir
param  |  param_id   |   point
sec    |  11         |   10
third  |  23         |   -20

sec    |   
id     |   uid
11     |   3

third  |   
id     |   uid
23     |   3
Copy after login
Copy after login

这个语句语法本身有问题吧。 from fir.param ... 但实际上 fir是表名,param是列名?
建议你不要写语句。把你的需求写出来,还有就是最终期望的执行结果写出来

楼主其实是把表名作为一个字段存起来了,然后根据条件找表名,最后取数据。这个操作无法在一条sql中完成,可以试试看视图或者存储过程。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template