I have a MySQL statement that I want to use that will display the data in two different tables, but not have any duplicated data.
SELECT Customer.firstName, Customer.lastName, Purchase.productName, Purchase.productPrice
FROM Purchase
INNER JOIN Customer
This is currently the MySQL I am using and it does work, but it loads duplicated data which I do not want. I have looked around but not seeing a simple solution. Sorry in advance if it is a simple solution, been working for awhile and brain isn't really working.
You have to bind those tables via related columns.
Let's assume primary column of table Customer is named ID and customer ID is being held in a column named customerID in table Purchase:
SELECT Customer.firstName, Customer.lastName, Purchase.productName, Purchase.productPrice
FROM Purchase
INNER JOIN Customer
ON Custormer.ID=Purchase.customerID
Related
I have 20 tables and want to see the data of all tables with one query. Is there any way I could do this?
The tables, join/converge on 2 columns. Also, is it possible to view the collective data directly on XAMPP?
It's completely based on how your tables connected to each other and you need a little knowledge to utilize your mysql server to do that for you.
In practice let's say you have a table for customers and a table for products and a relation between them indicating which customer bought which product (which also a table in this case):
Customers (id, username, password, fullname, email)
Products (id, name, sku, price)
Customers_Products (customer_id, product_id, created_at)
And we want to get a list of all products sold from May:
SELECT c.fullname, p.name, p.sku, p.created_at FROM Products AS p INNER JOIN Customers_Products AS cp ON p.id=cp.product_id INNER JOIN Customers as c ON c.id=cp.customer_id WHERE created_at > '2015-05-01 00:00:00';
I wrote this example to show you that doing this is completely depends on your requirements which can be very simple like selecting data from just one table to very complicated like joining multiple tables while filtering, grouping and ordering them.
For your next question. Yes It's possible you can see what's on your tables from phpMyAdmin but usually this is not a good solution since a user in phpMyAdmin can see the plumbing part of your system. The other way that most people use is to write some code to acquire the data and show them to the user.
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
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.
Here is my Database structure (basic relations):
I'm attempting to formulate a one-line query that will populate the clients_ID, Job_id, tech_id, & Part_id and return back all the work orders present. Nothing more nothing less.
Thus far I've struggled to generate this Query:
SELECT cli.client_name, tech.tech_name, job.Job_Name, w.wo_id, w.time_started, w.part_id, w.job_id, w.tech_id, w.clients_id, part.Part_name
FROM work_orders as w, technicians as tech, clients as cli, job_types as job, parts_list as part
LEFT JOIN technicians as techy ON tech_id = techy.tech_name
LEFT JOIN parts_list party ON part.part_id = party.Part_Name
LEFT JOIN job_types joby ON job_id = joby.Job_Name
LEFT JOIN clients cliy ON clients_id = cliy.client_name
Apparently, once all the joining happens it does not even populate the correct foreign key values according to their reference.
[some values came out as the actual foreign key id, not even
corresponding value.]
It just goes on about 20-30 times depending on largest row of a table that I have (one of the above).
I only have two work orders created, So ideally it should return just TWO Records, and columns, and fields with correct information. What could I be doing wrong? Haven't been with MySQL too long but am learning as much as I can.
Your join conditions are wrong. Join on tech_id = tech_id, not tech_id = tech_name. Looks like you do this for all your joins, so they all need to be fixed.
I really don't follow the text of your question, so I am basing my answer solely on your query.
Edit
Replying to your comment here. You said you want to "load up" the tech name column. I assume you mean you want tech name to be part of your result set.
The SELECT part of the query is what determines the columns that are in the result set. As long as the table where the column lives is referenced in the FROM/JOIN clauses, you can SELECT any column from that table.
Think of a JOIN statement as a way to "look up" a value in one table based on a value in another table. This is a very simplified definition, but it's a good way to start thinking about it. You want tech name in your result set, so you look it up in the Technicians table, which is where it lives. However, you want to look it up by a value that you have in the Work Orders table. The key (which is actually called a foreign key) that you have in the Work Orders table that relates it to the Technicians table is the tech_id. You use the tech_id to look up the related row in the Technicians table, and by doing so can include any column in that table in your result set.
I was thinking that I would have two tables for mysql. One for storing login information and the other for shipping address. Is that the conventional way or is everything store in one table?
For two tables... is there a way where it automatically copies a column from table A to table B, so that I can reference the same id to grab their shipping address...
If its a single address and if it is going to be updated everytime , then you can have it in a single table something like
**Customer**
customer_id [pkey]
customer_name
login_id
password
shipping_address
whereas if you want to store all the shipping addresses for a single customer(across multiple visits) then it would be a good design to have another table customer_shipping_address
**Customer**
customer_id [pkey]
customer_name
login_id
password
**Customer_Shipping_Address**
customer_id [fkey to customer]
shipping_address
This is my answer to your question regarding using 1 table or 2 tables. This decision depends on may factors. But i would suggest that you should use 2 separate tables. Because the log-in information is something that you will be retrieving very often compare to shipping information. Now if you have all the info in one table then table size will be huge and you will have to query this huge table everytime you need login information of user.
I think using two tables is better way to go. then just join them when you want to do the shipping.
The SQL for that would be like this.
SELECT
table1.id, table2.id, table2.somethingelse, table1.somethingels
FROM
table1 INNER JOIN table2
ON table1.foreignkey = table2.primarykey
WHERE
(some conditions is true)
The code above would need to be run on the shiping page itself.