So I made a basic struct of a database, and mysql filled some rows on a table and I don't know why. Maybe I can explain myself with the tables
mysql> show fields
-> from products;
+--------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+-------+
| ID_PRODUCT | decimal(8,0) | NO | PRI | NULL | |
| ID_CATEGORY | decimal(8,0) | NO | MUL | NULL | |
| NAME | text | NO | | NULL | |
| BRAND | text | NO | | NULL | |
| PICTURE | longblob | YES | | NULL | |
| OBSERVATIONS | text | YES | | NULL | |
| QUANT_UNIT | text | NO | | NULL | |
+--------------------+--------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
So this is a table with products, and I have one other table with the prices so I can save the history of prices
mysql> show fields from prices;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| ID_PRODUCT | decimal(8,0) | NO | MUL | NULL | |
| PRICE | text | NO | | NULL | |
| TIME | timestamp | NO | | NULL | |
+------------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
And if I insert the values into the product and prices tables
mysql> insert into products (id_product, id_category, name, brand, quant_unit)
-> values (1, 0, 'rice', 'XPTO', 'kg');
Query OK, 1 row affected (0.01 sec)
mysql> select * from products;
+------------+--------------+--------+----------+----------------+-------------+--------------------+
| ID_PRODUCT | ID_CATEGORY | NAME | BRAND | PICTURE | OBSERVATION | QUANT_UNIT |
+------------+--------------+--------+----------+----------------+-------------+--------------------+
| 0 | 0 | mug | no_brand | 0x | favorite | un |
| 1 | 0 | rice | xpto | 0x | NULL | kg |
+------------+--------------+--------+----------+----------------+-------------+--------------------+
2 rows in set (0.00 sec)
mysql> insert into prices (id_product, price, time)
-> values (0, '5', CURRENT_TIMESTAMP);
Query OK, 1 row affected (0.01 sec)
mysql> select * from prices;
+------------+-------+---------------------+
| ID_PRODUCT | PRICE | TIME |
+------------+-------+---------------------+
| 0 | 7.30 | 2020-11-27 23:25:34 |
| 1 | 5 | 2020-11-27 23:29:12 |
+------------+-------+---------------------+
2 rows in set (0.00 sec)
all fine so far, but if I do a select with both of the tables
mysql> select pro.id_product, pro.id_category, pro.name, pro.brand, pro.quant_unit, pre.price
-> from products pro, prices pri;
+------------+--------------+--------+----------+-------------+-------+
| id_product | id_category | name | brand | quant_unit | price |
+------------+--------------+--------+----------+-------------+-------+
| 0 | 0 | mug | no_brand | un | 7.30 |
| 1 | 0 | rice | xpto | kg | 7.30 |
| 0 | 0 | mug | no_brand | un | 5 |
| 1 | 0 | rice | xpto | kg | 5 |
+------------+--------------+--------+----------+-------------+-------+
4 rows in set (0.00 sec)
those are my constraints to connect the tables
alter table PRICES add constraint FK_PRICES_COST_PRODUCTS foreign key (ID_PRODUCT)
references PRODUCTS (ID_PRODUCT) on delete restrict on update restrict;
alter table PRODUCTS add constraint FK_PRODUCTS_PERTENCE_CATEGORY foreign key (ID_CATEGORY)
references CATEGORY (ID_CATEGORY) on delete restrict on update restrict;
I can't even search the problem because it doesn't make sense to me... But I'm new at SQL, so I think I'm doing something wrong...
I will appreciate any feedback, thanks!
I advise against using comma-join but in your query you didn't specify what to match between those two tables. You can simply fix that by adding WHERE
in your query like this:
select pro.id_product, pro.id_category, pro.name, pro.brand, pro.quant_unit, pre.price
from products pro, prices pri
where pro.id_product=pri.id_product;
But nowadays, most people uses JOIN
instead of comma-join, hence:
select pro.id_product, pro.id_category, pro.name, pro.brand, pro.quant_unit, pre.price
from products pro JOIN prices pri
ON pro.id_product=pri.id_product;
There's a lot of JOIN
types in MySQL and you can refer to this documentation.
well I'm using what my teachers teached me, but yea, that fixed my problem :O thanks mate!
I can understand that, I started with comma-join myself as a beginner. ;)
@tcadidot0 what do you have against comma-joins?? They look easier to read??
@YunfeiChen , ".. easier to read??" might not be true. Here, have a read at this or this, in fact there's a lot of article/post about comma vs join on the web. But, its up to you if you think its easier to read. I use comma-join in certain things that doesn't include matching fields.. but that also achievable using
CROSS JOIN
.@tcadidot0 I would be more worried about writing the whole statement on one line if I were you..... The where clause is quite shorter and easier to read for join as long as your are not joining let's say 15 or 20 tables together, but at that point it might just be better to normalize your tables....