I have two data sources, one that tracks products and one that tracks financial information. The two do not have a common field, but they both have a common field with our billing system. I want to use our billing system data as a bridge table between my two sources so that I can connect product data to financial data in one analysis without having to do a messy and slow blend in our viz tool (for example – using case logic to make product.client = finance.account…inconsistent naming conventions are the worst). I am basically using billing as a lookup table to first join product to billing and then join finance to my new join…I think.
I want to create three mysql tables with fields along the lines of the lettered items.
1. Product
a. client
b. prod_id
c. size
d. metric
2. Finance
a. account
b. fine_id
c. amount
d. country
3. Billing
a. account
b. prod_id
c. fine_id
I am using billing as a lookup table to first join product to billing so that I can get all my product fields plus account and fine_id. I then want to join finance to that first join. I can make it work in excel, but the scale of data we are working with once (if) this tool goes live is too big for excel, thus the need for MySQL. Am I correct that I can achieve this type of join on a join w/ MySQL? I would really appreciate any syntax guidance on joining my finance table to the join below. Thanks!
Edit to post - syntax for joining, what is the proper order of my joins? the error that I get is simply that there is an error with my syntax.I am using the below. I have reordered it a few times but I am still missing some conceptual piece of how to do this. I want to 1. join product to billing (returning rows even where there isn't a match) 2. join finance to the product/billing outer join so that I can see my product and finance data in the same table. thanks for all the help!
select *
from
(select *
from
(select * from
lookup.product) as product_join
left outer join
(select *
from lookup.billing) as billing_join
on product_join.product_id = billing_join.product_id) as left # left join of billing to product
left outer join
(select *
from left) as left_join
left outer join
(select * from lookup.finance) as finance_join
on left.finance_id = financef_join.finance_id) # joining of finance data to the left join of my product/billing
Your task/question: "I want to create three mysql tables
with fields along the lines of the
lettered items.
Product a. client b. prod_id c. size d. metric
Finance a. account b. fine_id c. amount d. country
Billing a. account b. prod_id c. fine_id
I am using billing as a lookup table to first join product to billing so that I can get all my product fields plus account and fine_id. I then ..."
That's possible and easy with a join:
SELECT * From product, billing, finance
WHERE billing.account = finance.account
AND billing.fine_id = finance.fine_id
AND billing.prod_id = product.prod_id
On respect of your comment, you can use the left outer join like this, if a correspondent billing data does not exist and you want all products in the results:
SELECT * from product
LEFT outer join billing
ON product.prod_id = billing.prod_id
LEFT outer join finance
ON billing.account = finance.account
AND billing.fine_id = finance.fine_id;
You don't have to put another "select *" around a join. You can just join the tables together like this.
Related
I am trying to create a MYSQL query that pulls in data from a range of tables. I have a master bookings table and an invoice table where I am recording invoice id's etc from Stripe.
I am storing two invoices per booking; one a deposit, the second the final balance.
In my admin backend I then display to the admin info on whether the invoice is paid etc so need to pull in data from SQL to show this.
I'm following some previous guidance here What's the best way to join on the same table twice?.
My query is returning data, however when the invoices table is included twice (to give me the deposit and balance invoices) however the column names are identical.
Could someone point me in the right direction? I think I need to somehow rename the columns on the second returned invoice??? Sorry new to anything but basic SQL queries.
This is my SQL
SELECT * FROM bookings
INNER JOIN voyages ON bookings.booking_voyageID = voyages.voyage_id
LEFT JOIN emailautomations ON bookings.booking_reference = emailautomations.automation_bookingRef AND emailautomations.automation_sent != 1
LEFT JOIN invoices ON bookings.booking_stripeDepositInvoice = invoices.invoice_id
LEFT JOIN invoices inv2 ON bookings.booking_stripeBalanceInvoice = inv2.invoice_id
Thanks to #Algef Almocera's answer I have amended my SQL (and stopped being lazy by using SELECT *, was able to trim loads of columns down to not many!)
SELECT
bookings.booking_status,
bookings.booking_reference,
bookings.booking_stripeCustomerReference,
bookings.booking_stripeDepositInvoice,
bookings.booking_stripeBalanceInvoice,
bookings.booking_totalPaid,
bookings.booking_voyageID,
bookings.booking_firstName,
bookings.booking_lastName,
bookings.booking_contractName,
bookings.booking_contractEmail,
voyages.voyage_id,
voyages.voyage_name,
voyages.voyage_startDate,
depositInvoice.invoice_id AS depositInvoice_id,
depositInvoice.invoice_status AS depositInvoice_status,
balanceInvoice.invoice_id AS balanceInvoice_id,
balanceInvoice.invoice_status AS balanceInvoice_status
FROM bookings
INNER JOIN voyages ON bookings.booking_voyageID = voyages.voyage_id
LEFT JOIN emailautomations ON bookings.booking_reference = emailautomations.automation_bookingRef AND emailautomations.automation_sent != 1
LEFT JOIN invoices depositInvoice ON bookings.booking_stripeDepositInvoice = depositInvoice.invoice_id
LEFT JOIN invoices balanceInvoice ON bookings.booking_stripeBalanceInvoice = balanceInvoice.invoice_id
This, sometimes, couldn't be avoided as keywords might actually often be the same but of different purpose per table. To help with that, you can use aliases. for example:
SELECT
invoices.column_name AS invoices_column_name,
transactions.column_name AS transactions_column_name
FROM invoices ...
LEFT JOIN transactions ...
I think there are something wrong with my structure. I have 3 tables in the database. I want to show all the supply in a specific division. I did some trial and errors but I can't insert another supply in the same plan id. I wanted to add more supplies in one of specific division / specific plan id.Here's my query to select the supplies.
SELECT
division.acronym,
supply.`name`,
supply.unit,
supply.supply_id,
supply.price,
supply.estimated_budget,
supply.quantity
FROM
division
INNER JOIN plan ON plan.plan_id = division.division_id
INNER JOIN supply ON supply.supply_id = plan.plan_id
The table is only available at documentation. So, I insert my ERD diagram as an image.
ERD edited
I think you should put division_id and supply_id to plan table as FOREIGN KEYS to join all three tables.
Then your query should look like:
division
INNER JOIN plan ON plan.division_id=division.division_id
INNER JOIN supply ON supply.supply_id=plan.supply_id
I'm taking a database class and I'm having troubles with using the JOIN command to get the data I need. I hope I'm on the right track...I would appreciate any inputs if there is a better way both in my model and query.
Sales transaction that each employee has processed
, I'm trying to answer the following question:
Here is a query I tried but fail to come up with correct output. I had to use a table alias for sales because I kept getting unique errors. I only have 10 rows in my bridging table however, I got 100 results back...JOIN statements are kicking my butt right now.
SELECT sales.cashier, inventory.prod_name, inventory.unit_price
FROM inventory, sales
JOIN inv_sales ON inventory_prod_id = inventory.prod_id
JOIN sales AS sales1 ON sales_sales_id = sales_id;
edit: Additional requested data
Here is a screenshot of some sample data.
I had to put them all on one pic due to site restrictions.
Tables:
Top, Inv_sales
Middle, Sales
Bottom, Inventory
Expected output:
cashier > product name > unit price
You might try something like this:
SELECT s.cashier, i2.prod_name, i2.unit_price
FROM sales s
JOIN inv_sales i1
ON i1.sales_sales_id = s.sales_id
JOIN inventory i2
ON i1.inventory_prod_id = i2.prod_id
ORDER BY s.cashier,s.date;
It uses a unique alias for each table, and uses the modern form of joining tables. Also, since the requirement was to get the sales for each employee, I added an order by so an employees sales would be grouped together.
I need to perform a query SELECT that joins three tables (no problem with that). Nonetheless, the third table can, or NOT, have any element that match the joining KEY.
I want ALL data from the first two tables and if the ITEMS have ALSO information in the third table, fetch this data to.
For example, imagine that the first table have a person, the second table have his/her address (everyone lives anywhere), the third table stores the driving license (not everyone has this) - but I need to fetch all data whether or not people (all people) have driving license.
Thanks a lot for reading, if possible to give you suggestion / solution!
Use LEFT JOIN to join the third table. Using INNER JOIN a row has to exists. Using LEFT JOIN, the 'gaps' will be filled with NULLs.
SELECT
p.PersonID, -- NOT NULL
-- dl.PersonID, -- Can be null. Don't use this one.
p.FirstName,
p.LastName,
a.City,
a.Street,
dl.ValidUntilDate
FROM
Person p
INNER JOIN Addresse a ON a.AddressID = p.HomeAddressID
LEFT JOIN DrivingLicence dl ON dl.PersonId = p.PersonID
I have a query I need to perform on a table that is roughly 1M records. I am trying to reduce the churn, but unfortunately there is a UNION involved (after i figure this join out), so that may be a question for another day.
The records and data I need to get reference 3 fields in a table that need each pull a description from another table and return it in the same record, but when i do the Inner join i was thinking, it either returns only 1 field fromt he other table, or multiple records from he original table.
Here are some screen shots of the tables and their relationship:
Primary table containing records (1 each) with the physician record I want to pull, including up to 3 codes that can be listed in the "taxonomy" table.
Secondary table containing records (1 each) with the "Practice" field I want to pull.
A Quick glance of the relationship i'm talking about
I presume that if perform an inner join matching the 3 fields in the physicians table, that it will have to iterate that table multiple times to pull each taxonomy code .. but I still can't even figure the syntax to easily pull all of these codes instead of just 1 of them.
i've tried this:
SELECT
taxonomy_codes.specialization,
physicians.provider_last_name,
physicians.provider_first_name,
physicians.provider_dba_name,
physicians.legal_biz_name,
physicians.biz_practice_city
FROM
taxonomy_codes
INNER JOIN physicians ON physicians.provider_taxonomy_code_1 = taxonomy_codes.taxonomy_codes OR physicians.provider_taxonomy_code_2 = taxonomy_codes.taxonomy_codes OR physicians.provider_taxonomy_code_3 = taxonomy_codes.taxonomy_codes
First, the query churns a lot and it only returns one taxonomy specialty result which I presume is because of the OR in the join statement. Any help would be greatly appreciated.
Thank you,
Silver Tiger
You have to join the taxonomy_codes table multiple times:
SELECT p.provider_last_name, p...., t1.specialization as specialization1, t2.specialization as specialization2, t3.specialization as specialization3
FROM physicians p
LEFT JOIN taxonomy_codes t1 ON t1.taxonomy_codes = provider_taxonomy_code_1
LEFT JOIN taxonomy_codes t2 ON t2.taxonomy_codes = provider_taxonomy_code_2
LEFT JOIN taxonomy_codes t3 ON t3.taxonomy_codes = provider_taxonomy_code_3