Databases, intermediate table relations

0

First of all I do not have much knowledge about MySQL, nor databases in general, so I regret the inconvenience that may cause.

I have a database with two tables, registered, and product, registered has attributes such as ID (primary key), the registered user, your password and your email, on the other hand the product table has another ID attribute (primary key) ), the name of the product, the price and a brief description of it.

What interests me is having an intermediate table where you can store ONLY, the username and the respective price.

Such that:

Juan 200
Pablo 129
Juan 19
Raquel 15

That is, a name can appear repeatedly.

Now, what would you need in the middle table, and with what could you relate it? This is the main problem.

Finally, if in the intermediate table I would like to enter data using PHP PDO, it would be as simple as sending a query that makes an insert into that table, right?

Thanks for your answers.

    
asked by Omar 29.04.2018 в 19:43
source

1 answer

0

What you mention, according to my experience, is not possible. You must relate it to some field.

Even if it is an id field.

Example:

Intermediate Table:

id int auto incremental primary_key
usuario text
valor int
id_usuario int
id_producto int

Query:

SELECT usuario, valor FROM intermedia WHERE id_usuario = x OR id_producto = x

You could improve the table even further, regardless of the user and value fields, since with the registered id and products you have these data in their respective tables, if that is how you are clear ...

Query:

SELECT registrados.nombre, productos.valor FROM intermedia INNER JOIN registrados ON registrados.id = intermedia.usuario_id INNER JOIN productos ON productos.id = intermedia.producto_id WHERE registrados.id = x

Starting with the WHERE, it's a question of what you need ... as if you dispense with it and it will return all the results relating the two tables, products and registered.

Greetings.

    
answered by 29.04.2018 в 21:33