There is such a query to the database:

select * from `queue` left join `clients` on `clients`.`id` = `queue`.`client_id` where `queue`.`user_id` = '11' and `queue`.`user_id` is not null and `finished` = '0' 

But after left join, the id key from the table to which I join is replaced with the key of the table that I join. Have a solution without renaming keys?

  • What dialect request? - 0xdb
  • Well, if I understood the question correctly, mysql - Oleg Halin
  • Add a mysql label. Add instead of "*" fields that interest you. - 0xdb
  • The fact is that using the laravel hasMany function by key, if I specify the fields, the key search will not work. In addition, we need the field of the first table id - Oleg Halin
  • where queue.user_id = '11' and queue.user_id is not null The second condition is meaningless, it can be removed from the query. ключ id из таблицы к которой присоединяю заменяется на ключ таблицы которую присоединяю I wonder how you distinguish them if they are equal? Or do you want to say that queue.client_id is a Null value for queue entries that are not matched by clients? - Akina

1 answer 1

You can specify an alias for the clients.id field or not select it at all, but the remaining required fields from the clients table must be explicitly specified:

 select Q.*, C.id as c_id, C.поле1, C.поле2, C... from queue Q left join clients C on C.id = Q.client_id where Q.user_id = '11' and Q.finished = '0';