Data from two tables with the same column names

I have a table for users. But when the user makes any changes to their profile, I save them in the pace table until I approve them. Then the data is copied to the current table and deleted from the temp table.

What I want to achieve is that when viewing data in the admin panel or on a page where the user can double-check before sending, I want to write one query that will allow me to retrieve data from both tables where id in both equals $ userid. Then I want to display them in a table, where the old value appears in the left column and the new value appears in the right column.

I found several sql solutions, but I'm not sure how to use them in php to echo the results, since the columns in both have the same name.

+3
source share
4 answers

Adding ASa column to the name will allow you an alias with a different name.

SELECT table1.name AS name1, table2.name AS name2, ...
  FROM table1
  INNER JOIN table2
    ON ...
+7
source

Here is your requested request. Suppose you have, for example, a name field in two tables. Table 1 for logging in and table 2. Now

SELECT login.name as LoginName , information.name InofName 
FROM login left join information on information.user_id = login.id

Now you can use LoginNameand InofNamein any place.

+1
source

AS SQL, .

SELECT
    `member.uid`,
    `member.column` AS `oldvalue`,
    `edit.column` AS `newvalue`
FROM member, edit
WHERE
    `member.uid` = $userId AND
    `edit.uid` = $userId;

Something in this direction should work for you. Although SQL is not my forte, I’m sure that this query will not work as it is, even in a table with the correct fields and values.

+1
source

Use MySQL JOIN. And you can get all the data from 2 tables in one mysql query.

SELECT * FROM `table1`
JOIN `table2` ON `table1`.`userid` = `table2`.`userid`
WHERE `table1`.`userid` = 1
0
source

All Articles