MYSQL - 从表中选择全部如果
我有一个(很好的)mysql 表,如下所示:
Fields Datas
id (pk) 1 2 3 4 5 6
master_id 1000 1000 1000 2000 2000 2000 ...
master_name home home home shop shop shop ...
type_data value common client value common client ...
param_a foo_a 1 0 bar_a 0 1 ...
param_b foo_b 1 0 bar_b 1 0 ...
param_c foo_c 0 1 bar_c 0 1 ...
... ... ... ... ... ... ... ...
所有这些数据都嵌入在一个表中。每个数据都分派到 3 个“列”集(1 个用于值,1 个用于识别这些值是否为通用值,1 个用于识别客户端值)。这不是我得到的最好的,但许多其他脚本都依赖于这种结构。
我需要这样的东西:
SELECT parameters name (eg param_a, param_b..) and their values (eg foo_a, foo_b..)
WHEN master_id=? AND type_data=(common or client) (eg for values=1 on the 2nd column)
。
为了获取特定 master_id 的参数哈希,就像
param_a => foo_a
param_b => foo_b
param_c => foo_c
...
我到目前为止无法成功地在同一个表上自连接一样,但我想它应该是可行的。 (我想避免进行多次查询)
提前谢谢
I have a (nice) mysql table built like this :
Fields Datas
id (pk) 1 2 3 4 5 6
master_id 1000 1000 1000 2000 2000 2000 ...
master_name home home home shop shop shop ...
type_data value common client value common client ...
param_a foo_a 1 0 bar_a 0 1 ...
param_b foo_b 1 0 bar_b 1 0 ...
param_c foo_c 0 1 bar_c 0 1 ...
... ... ... ... ... ... ... ...
All these datas are embed in a single table. Each datas are dispatched on 3 "columns" set (1 for the values, 1 for identifying if these are common values and one for identifying client values). It's not the best I got but many other scripts depends on this structure.
I'd need sthg like this:
SELECT parameters name (eg param_a, param_b..) and their values (eg foo_a, foo_b..)
WHEN master_id=? AND type_data=(common or client) (eg for values=1 on the 2nd column)
.
in order to get the parameters hash for a particular master_id like
param_a => foo_a
param_b => foo_b
param_c => foo_c
...
I could not succeed in self joining on the same table till now but I guess it should be feasible. (I'd like to avoid to do several queries)
Thx in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您正在谈论旋转数据?如果是这样,请参阅此处:http://en.wikibooks.org/wiki/MySQL/Pivot_table 和这里: http://dev.mysql.com/tech-资源/文章/向导/print_version.html
I think you are talking about pivoting the data? If so, see here: http://en.wikibooks.org/wiki/MySQL/Pivot_table and here: http://dev.mysql.com/tech-resources/articles/wizard/print_version.html