在MySQL中查找包含给定值的列 [英] Find column that contains a given value in MySQL

查看:71
本文介绍了在MySQL中查找包含给定值的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MySQL数据库中有一个表.给我一个值,该值作为该表中的单元格值出现,但我不知道它是哪个单元格,即该单元格的行和列.查找该值所属列的最有效方法是什么?预先感谢.

I have a table in a MySQL database. I am given a value that occurs as a cell value in that table but I do not know which cell is it i.e. the row and column of that cell. What is the most efficient way to find the column to which that value belongs? Thanks in advance.

示例:

Column_1 | Column_2 | Column_3
1        | 2        | 3
4        | 5        | 6
7        | 8        | 9

现在,我得到的输入值为"8".我想知道是否有一种有效的方法来找出"8"的值属于Column_2.

Now I am given an input value of "8". I want to know if there is an efficient way to find out that value of "8" belongs to Column_2.

推荐答案

您不知道数据位于哪一列,这有点奇怪,因为列的目的是具有明确定义的功能.

It's a bit strange that you don't know which column the data is in, since columns are meant to have a well-defined function.

[原始回复已清除.]

[Original response scrubbed.]

编辑:您更新的帖子仅要求提供该列.在这种情况下,您不需要视图,只需运行该查询

Your updated post just asks for the column. In that case, you don't need the view, and can just run this query

SELECT col FROM (
   SELECT "Column_1" AS col, Column_1 AS value FROM YourTable
   UNION ALL SELECT "Column_2", Column_2 FROM YourTable
   UNION ALL SELECT "Column_3", Column_3 FROM YourTable
) allValues
WHERE value=8;

对表运行此查询时,它将返回"Column_2"

When you run this query against your table, it will return "Column_2"

这篇关于在MySQL中查找包含给定值的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆