Catch id auto increment of two joined tables (INNER JOIN)

Asked

Viewed 50 times

-1

Let’s see if I can explain at first...

have two tables [topics] and [notification]

I joined these tables with INNER JOIN to get the data of both, so far so good...

but when I need to select the [id] of the [notification] table in while, but only return me the [id] of the [topic] table, how can I recover the id of both in that select?

Follow my SELECT:

SELECT notification.*, topicos.* FROM notification INNER JOIN topicos ON notification.user = "USUARIO_TAL" 
  • the primary key of the two tables is id?

  • yes the primary key of the two is [id].

1 answer

1


Ideally you list the columns that matter one by one, instead of using *. There you can define a alias for each one if necessary. For example:

SELECT
    notification.id AS notification_id,
    topicos.id AS topico_id
...

In his while (in php, for what you said), you can refer to id by alias, in case notification_id or topico_id.

  • understood, however are many fields would not like to leave the code much bigger in this case, as I can recover the variable inside the while?

  • Well, nothing stops you from doing SELECT notification.*, notification.id AS notification_id etc.

  • understood, but when listing the ids inside while I can’t recover both ids, if I do while ($data = $select->fetch()){$data['id']} it returns the first but not the second..

  • can solve, I made the select as told me setando Alias for the fields and at the time of recovering no while I call the alias field instead of the original field, thanks there bfavaretto

  • That, I’m sorry, I should have explained that part. I’ll edit the answer to make it complete.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.