How to show a column from one table in another table - mysql

I have two tables:
One table is called item, which among other things contains item_name.
The second table that I have is called recipe, which has 4 rows: recipe_id, item_id, nom_netto and tolerance.
Is there a way to temporarily show the item_name column in the recipe table?
If not temporarily, how would I go about to do it permanently?
Thanks in advance.

You can do that by using INNER JOIN.
Please see this example: https://www.w3schools.com/sql/sql_join_inner.asp
Please also see this to understand how different types of JOINS work: https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

Related

How can I combine these two tables so that I can sort with information on each table, but not get duplicate answers?

I have two tables. The first is named master_list. It has these fields: master_id, item_id, name, img, item_code, and length. My second table is named types_join. It has these fields: master_id and type_id. (There is a third table, but it is not being used in the queries. It is more for reference.) I need to be able to combine these two tables so that I can sift the results to only show certain ones but part of the information to sift is on one table and the other part is on the other one. I don't want duplicate answers.
For example say I only want items that have a type_id of 3 and a length of 18.
When I use
SELECT * FROM master_list LEFT JOIN types_join ON master_list.master_id=types_join.master_id WHERE types_join.type_id = 3 AND master_list.length = 18"
it finds the same thing twice.
How can I query this so I won't get duplicate answers?
Here are the samples from my tables and the result I am getting.
This is what I get with an INNER JOIN:
BTW, master_id and name both only have unique information on the master_list table. However, the types_join table does use the master_id multiple times later on, but not for Lye. That is why I know it is duplicating information.
If you want unique rows from master_list, use exists:
SELECT ml.*
FROM master_list ml
WHERE ml.length = 18 AND
EXISTS (SELECT 1
FROM types_join tj
WHERE ml.master_id = tj.master_id AND tj.type_id = 3
);
Any duplicates you get will be duplicates in master_list. If you want to remove them, you need to provide more information -- I would recommend a new question.
Thank you for the data. But as you can see enter link description here, there is nothing wrong with your query.
Have you tried create an unique index over master_id, just to make sure that you do not have duplicated rows?
CREATE UNIQUE INDEX MyMasterUnique
ON master_list(master_id);

SQL Table for Groups ? Which solution would be better?

I'm trying to setup groups in my network using MySQL. Users could join in groups or create groups. Of the two options below, I'm trying to identify which solution is better.
First Solution:
I create a Table called "Groups" with columns:
- GroupID
- GroupName
- Createtime
- GroupMembers...
In this solution I would save the users in the column GroupMembers like this "user1;/user2;/" and so on. This is my first solution; I don't know if this would be faster than the second solution. I just want to lookup GroupMembers, etc.
Second Solution:
I would create a table like in the first solution, but without the column GroupMembers, and I would create a second table for the GroupMembers.
This table will have following columns:
-GroupID //The ID of the group in the other table.
-UserID
When joins a group, it will be stored in the above table.
The first solution is Bad because it breaks normalization.
The second solution is Good.
The typical approach would be to have a Users table, a Groups table and a UserGroups (or GroupUsers) table

How can I show values from two tables with one SELECT command in SQL?

I have two tables. First table has food items with prices on it. The other table has orders of foods on it. Both tables have the same named food items. What I would like to do is, have the price show as an extra column behind the particular food item in one table.
I don't even know how to search for this, I tried and failed miserably. So now I am turning to people who are most likely better at SQL.
Hope someone understands my problem and is able to help me.
Use a join:
select orders.*, prices.price
from orders
join prices on prices.foodName = orders.foodName

SQL have one column in two tables

I am trying to create a table that shows treatment information about patients (though I just wondered if would be better as a query) at a fictional hospital. The idea is that one row of this could be used to print an information sheet for the attending nurse(s).
I would like to make the attending_doctor column contain the name that corresponds with the employee_id.
|Patient_ID|Employee_ID|Attending_Doctor|Condition|Treatment|future_surgery|
Would appreciate any help. Thank you!
Just use a join in your query rather than have the employee name in 2 tables (which would mean updating in more than one location if they change name etc). For the sake of an example, this also gets the patients name from a 3rd table named patients.
eg
SELECT table1.*, employees.name, patients.name
FROM table1
LEFT JOIN employees ON employees.id = table1.employeeId
LEFT JOIN patients ON patients.id = table1.patientsId
Don't use directly this table, but build a view that contains the data you need. Then you can get the data from the view like it was a table.
Basically what you need is to have data in three tables. One table for patients, one table for for employees and one for the reports. Table with reports should contain only the employee_ID. Then you can either build a direct query over these three tables or build a view that will hide the complicated query.

MySQL Cross Join with concat field

I have 2 tables in a MySQL database: Products and ProductsType.
Each element of the Products table has a type which is contained in ProductsType table.
Each table has a field named "code".
I'd like to have a VIEW with the cartesian product of these 2 tables and I know that I can do it with a CROSS JOIN.
But I'd like that in this VIEW I can see the CONCAT of the 2 fields named "code" of my 2 tables. Is that possible?
I've also thought that I can make 3 tables, Products, ProductsType and ProductsWithType and in the first 2 I put some triggers "after insert" that keep updated the third table ProductsWithType, but I'd like to know if it is possibile to do with a view.
Thanks in advance
Are you looking for this?
CREATE VIEW vw_products_types AS
SELECT CONCAT(p.code, t.code) combined_code, p.product_name, t.type_name
FROM Products p CROSS JOIN ProductsType t
Here is SQLFiddle demo