D
D
daxak2022-04-21 18:59:36
MySQL
daxak, 2022-04-21 18:59:36

How to get one field data from two mysql tables?

There is a `messages` table with an id (message id) field and a `users` table with an id (user id) field. How to get data from two tables. My attempt: I get: ERROR 1052 (23000): Column 'id' in field list is ambiguous
SELECT id FROM `messages`,`money_box`;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2022-04-21
@daxak

SELECT 
  `messages`.`id` AS `messages_id`, `money_box`.`id`  AS `money_box_id`
FROM `messages`,`money_box`;

https://sqlize.online/sql/mysql57/bfb655473f8cb778...

D
Dmitry, 2022-04-21
@Compolomus

the problem is that as a result the id is from both tables, therefore a conflict is obtained,
and it can also be misunderstood with such a query, it is better to use join

SELECT 
  `messages`.`id` AS `messages_id`, `money_box`.`id`  AS `money_box_id`
FROM `messages` JOIN `money_box`

then you can already modify the query by specifying how to connect the tables, by which fields, and also specify the conditions for sampling, sorting, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question