How to have more than one value for another table?

Asked

Viewed 48 times

-1

Good people, I come with a doubt that has been chasing me for a long time.

Let’s get to it:

I have the table X however values of table X I picked up at table Y. How to store more than one value in table Y concerning table X whereas the table Y can also reference with the same values for other tables.

With that I have a problem that I really need help.

What do I think? Create another table to help these values for example:

I have the table X and the table Y. I create the table Z where it will store all that X need in the table Y so in case I have a table G which also requires an item from table Y, I will arrange for table Z all items that G needs.

I need to know if this thought is correct or if there is another way to accomplish it.

Guys, thanks in advance. Thanks for helping me.

  • Peter, I’m sorry, but your question is very confusing. Many references from X Y G Z. Wouldn’t you be able to post the tables and some data to simplify the story? As soon as you simplicate I withdraw my negative vote.

  • Relax, I don’t care about your negative vote. This only discourages the interest of those who seek help, if you first request change in the question before denying it. but all right, I’m already used to the oldest lie in the country. that power can be innocent

  • This has nothing to do with power. I just wanted to show you that your question is complex to understand. This is a community, which in my view aims to improve the knowledge of all and nothing to do with power.

  • My question has been answered, perhaps your understanding has not been sufficient and your power can lead to it being negative or positive. Not always your view is right.

1 answer

1


By my understanding of your question, you want to pull the data from two tables in one query, is that it? If it is, in the Thiago Belem there’s a great article that’s where I learned, and here has a little more on the subject.

Example: You have two tables in the database with some data entered

CREATE TABLE `categorias` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `nome` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM;

INSERT INTO `categorias` VALUES
(1, 'Camisetas'),
(2, 'Canecas');

CREATE TABLE `produtos` (
    `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `categoria_id` INT NOT NULL ,
    `nome` VARCHAR( 255 ) NOT NULL ,
    `preco` DECIMAL( 10,2 ) NOT NULL
) ENGINE = MYISAM;

INSERT INTO `produtos` VALUES
(1, 1, 'Camiseta Social', 15.00),
(2, 1, 'Camiseta Regata', 11.99),
(3, 2, 'Caneca Grande', 12.00);

And it wants a return more or less like this:

| id | categoria_id | nome            | preco | categoria |
| 2  | 1            | Camiseta Regata | 11.99 | Camisetas |
| 1  | 1            | Camiseta Social | 15.00 | Camisetas |
| 3  | 2            | Caneca Grande   | 12.00 | Canecas   |

You should mount a QUERY like this:

SELECT p.*, c.`nome` AS categoria FROM `produtos` AS p
INNER JOIN `categorias` AS c ON p.`categoria_id` = c.`id`
ORDER BY p.`nome` ASC

Where INNER JOIN will merge the table categories in the table search products. That I believe, is your doubt.

(Source: Thiago Belem)

  • Full answers are recommended if links or use them only to supplement the question.

  • I already solved my problem. I created another table to save the reference where the same value can be used several times. A table to help this solved ! hehe .. but thank you very much only with this logic that you gave me. I thank you very much!

Browser other questions tagged

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