I have a db with 8 tables which intend to search for an id number by joining them but i would like to retrieve the name of the table to use in a php along the lines of...
我有一个带有8个表的数据库,它打算通过加入它们来搜索一个id号,但我想检索要在php中使用的表的名称......
if($tablename == 'accounts'){
echo 'value found in accounts';
}
What would the mysql query be?
mysql查询是什么?
(I don't need help with the joining etc, just the bit that would get the table name and how to store it in $tablename)
(我不需要帮助加入等,只需要获取表名以及如何将其存储在$ tablename中)
Thanks
谢谢
3 个解决方案
#1
1
How can you search for data in a table if you don't know the table name in the first place?
如果您首先不知道表名,如何在表中搜索数据?
So I suppose that you already know the table names, and don't need things like SHOW TABLES
; that you have e.g. a WHERE
of the form
所以我想你已经知道了表名,不需要像SHOW TABLES这样的东西;你有...表格中的哪一个
WHERE (table1.field LIKE '%search%' OR table2.field2 LIKE '%search%' OR ...)
and once retrieved a row, you know it matches, but you don't know where the match did occur. And running eight straight queries instead of one JOINed isn't an option.
并且一旦检索到一行,您就知道它匹配,但您不知道匹配发生的位置。并且不能选择运行八个直接查询而不是一个JOINed。
If this is the case, you'll have to supplement your SELECTed fields with a flag to tell you what you need:
如果是这种情况,您必须使用标记来补充SELECTed字段,以告诉您需要的内容:
SELECT ..., CASE
WHEN accounts.field1 LIKE '%search%' THEN 'accounts'
WHEN customers.name LIKE '%search%' THEN 'customers'
...
END AS wtf FROM <rest of your query>
Then in PHP when retrieving the row, you will just use the field:
然后在PHP中检索行时,您将只使用该字段:
print "Value found in {$row['wtf']}";
#2
2
SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name'
19.12. The INFORMATION_SCHEMA TABLES Table
19.12。 INFORMATION_SCHEMA TABLES表
#3
0
'SHOW TABLES' afair, is it still used on mysql? :)
'SHOW TABLES'不公平,它还在mysql上使用吗? :)
#1
1
How can you search for data in a table if you don't know the table name in the first place?
如果您首先不知道表名,如何在表中搜索数据?
So I suppose that you already know the table names, and don't need things like SHOW TABLES
; that you have e.g. a WHERE
of the form
所以我想你已经知道了表名,不需要像SHOW TABLES这样的东西;你有...表格中的哪一个
WHERE (table1.field LIKE '%search%' OR table2.field2 LIKE '%search%' OR ...)
and once retrieved a row, you know it matches, but you don't know where the match did occur. And running eight straight queries instead of one JOINed isn't an option.
并且一旦检索到一行,您就知道它匹配,但您不知道匹配发生的位置。并且不能选择运行八个直接查询而不是一个JOINed。
If this is the case, you'll have to supplement your SELECTed fields with a flag to tell you what you need:
如果是这种情况,您必须使用标记来补充SELECTed字段,以告诉您需要的内容:
SELECT ..., CASE
WHEN accounts.field1 LIKE '%search%' THEN 'accounts'
WHEN customers.name LIKE '%search%' THEN 'customers'
...
END AS wtf FROM <rest of your query>
Then in PHP when retrieving the row, you will just use the field:
然后在PHP中检索行时,您将只使用该字段:
print "Value found in {$row['wtf']}";
#2
2
SELECT table_name FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = 'db_name'
19.12. The INFORMATION_SCHEMA TABLES Table
19.12。 INFORMATION_SCHEMA TABLES表
#3
0
'SHOW TABLES' afair, is it still used on mysql? :)
'SHOW TABLES'不公平,它还在mysql上使用吗? :)