简体   繁体   中英

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.

For each row of data I have a userid as one of the field. 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?

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?

Thanks In Advance

You have to use where clause in your mysql statement

where 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.

(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

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 . 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.

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       |
+------------+----------+--------------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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