简体   繁体   English

在PHP mysql中显示数据库中所有表中具有特定ID的行

[英]Display rows with a specific ID from all tables in a database in PHP mysql

Currently I have a big database with like 5+ tables and lot of data within. 目前,我有一个大型数据库,其中包含5个以上的表,并且其中包含大量数据。

For each row of data I have a userid as one of the field. 对于每一行数据,我都有一个userid作为该字段之一。 I have it this way so that can identify which data belongs to which userid. 我有这种方式,以便可以识别哪些数据属于哪个用户标识。

Now how do I display out all the data from all the tables with a specific userid? 现在如何显示具有特定用户ID的所有表中的所有数据?

I have tried some examples which i found online which displays out all the data using information_schema.tables but how do I only display from a particular userid? 我尝试了一些示例,这些示例是我在网上找到的,这些示例使用information_schema.tables显示所有数据,但是如何仅从特定的用户ID显示呢?

Thanks In Advance 提前致谢

You have to use where clause in your mysql statement 您必须在mysql语句中使用where子句

where userid = this 其中userid = this

put this at end of your statement 将此放在您的声明结尾

I'm afraid there's no simple way how to do this, you can: 恐怕没有简单的方法可以做到这一点,您可以:

Run multiple queries: 运行多个查询:

Run one query for each type of table (articles, comments, posts... ): 对每种类型的表格(文章,评论,帖子...)运行一个查询:

display_articles_from_select( 'SELECT FROM articles WHERE user_id ... ');
display_comments_from_select( 'SELECT FROM comments WHERE user_id ... ');

UNION

This option is available only if columns in each table are "compatible" to each other, for example each table has title and text field. 仅当每个表中的列彼此“兼容”时(例如,每个表都有titletext字段),此选项才可用。

(SELECT title, text FROM articles WHERE used_id = ...)
UNION
(SELECT header AS title, text FROM comments WHERE used_id = ...)
...

JOIN

And if you have database schema which allows this you can simply join tables, but without detailed DB scheme info I can't provide an example nor confirm that this is possible in your case. 而且,如果您具有允许这样做的数据库模式,则可以简单地联接表,但是如果没有详细的数据库方案信息,我将无法提供示例,也无法确认您是否可以这样做。

basic syntax for selecting all data from different tables: 从不同表中选择所有数据的基本语法:

table1, table2, table3 table1,table2,table3

SELECT t1.*, t2.*, t3.* 
FROM table1 as t1, table2 as t2, table3 as t3
WHERE
t1.userid = '1' and t2.userid = '1' t3.userid = '1'

You can see this site for more information 您可以查看此网站以获取更多信息

this one will be achieved by the JOIN Queries . 这将通过JOIN查询来实现。 if u have any doubt please see this link. 如果您有任何疑问,请参阅此链接。 http://phpweby.com/tutorials/mysql/32 or o/w give me the table fields i will update. http://phpweby.com/tutorials/mysql/32或o / w给我我将更新的表字段。

and i hope this will help you.. 我希望这对您有帮助。

for example, 例如,

1). mysql> SELECT * FROM products;
+----+--------------+--------------+
| id | product_name | manufacturer |
+----+--------------+--------------+
|  1 | Shoes        | Company1     |
|  2 | Laptop       | Company2     |
|  3 | Monitor      | Company3     |
|  4 | DVD          | Company4     |
+----+--------------+--------------+
4 rows in set (0.00 sec)





 2). mysql> SELECT * FROM buyers;

    +----+------+------------+----------+
    | id | pid  | buyer_name | quantity |
    +----+------+------------+----------+
    |  1 |    1 | Steve      |        2 |
    |  2 |    2 | John       |        1 |
    |  3 |    3 | Larry      |        1 |
    |  4 |    3 | Michael    |        5 |
    +----+------+------------+----------+

this will be the output. 这将是输出。

mysql> SELECT buyers.buyer_name, buyers.quantity, products.product_name FROM buyer
s LEFT JOIN products ON buyers.pid=products.id where buyers.id=1;
+------------+----------+--------------+
| buyer_name | quantity | product_name |
+------------+----------+--------------+
|John        |        1 | Laptop       |
+------------+----------+--------------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM