Is it possible to compare data from two tables at once in PCP? For example, there is a table in the users database and in the same comments database. The first table has an id field, and the second has a com_id. Is it possible to compare them if you connect both tables at once in the query?
- It's not entirely clear what you want to do. What do you want to compare? Maybe you meant to unite? So it does not depend on PHP. In the database query, you can combine two tables (or more), for example from your example user.id and comment.com_id, as a result, it will be possible to get users and related comments (unless of course this logic was embedded in the database) . Then, using PHP, it will be possible to process this data. - uvlad
|
1 answer
<? $com_id=15; $id = 1; $q = '' .'SELECT *' .' FROM users AS T1' .' JOIN comments AS T2' .' WHERE T1.id='.$id .' AND T2.com_id='.$com_id .' LIMIT 1;'; $res = mysql_query($q); if ($result = mysql_fetch_object($res)) var_dump($result); ?>
In general, yes, you can also without joins. You can compare and direct treatment:
[ ... ] WHERE users.id = comments.com_id [...]
- oneMultiple SELECT from multiple tables in MySQL is the same as JOIN. - Dex
|