MySQL table Join for Multiple Values - mysql

I have 2 tables, one with 2 columns of product ids (old and new) that need to mapped to each other and another table with all the product info, including a corresponding product id. I am trying to run a query to double check that the products are matched correctly and it is not working, I need the returned results to show the old id and newid along with the full product info.
Here is what I cobbled together from some other queries I use, but I am not sure I am doing the double join correctly.
SELECT `VinylOld2New`.`Old ID`, `asi_VinylOld2New`.`New ID`, old.`title`, new.`title`, old.`number`, new.`number`, old.`image`, new.`image`, old.`added`, new.`added`
FROM `ii_Product` old, `ii_Product` new
INNER JOIN `VinylOld2New` ON
old.`id` = `VinylOld2New`.`Old ID` OR new.`id` = `VinylOld2New`.`New ID`

Pick a standard. Use INNER JOIN or , notation both both. (preferably the newer standard)
I'm not sure how you want to join the data here. Your use of OR seems to imply you want all records from vinylOld2New and only those that match in ii_product (old or new) but that's a guess.
Your 2nd column in select indicates asi_VinylOld2New.New ID but there is no such table. So is the table in the select wrong or the one in the from?
.
SELECT `VinylOld2New`.`Old ID`
, `VinylOld2New`.`New ID`
, old.`title`
, new.`title`
, old.`number`
, new.`number`
, old.`image`
, new.`image`
, old.`added`
, new.`added`
FROM `VinylOld2New`
LEFT JOIN `ii_Product` old
ON old.`id` = `VinylOld2New`.`Old ID`
LEFT JOIN `ii_Product` new
ON new.`id` = `VinylOld2New`.`New ID`

Related

MySQL syntax errors of equi join

i want to solve these two problems with equi join
For each customer order, list the order id, order date, order_source_id, source description, and the first and last name of the customer.
For each line in shipment_line, display the shipment_id, inv_id, ship_quantity, date_expected and date_received.
This is my syntax but it is showing error.
1.
SELECT order_id,order_date,order_source_id,source_desc,first,last
FROM customer,cust_order,order_source
WHERE (cust_order.order_source_id = order_source.order_source_id)
AND cust_order.cust_id = customer.cust_id;
2.
SELECT shipment_line.shipment_id,shipment_line.inv_id,shipment_line.date_recieved,shipment.date_expected
FROM shipment_line, shipment
WHERE shipment_line.shipment_id = shipment.shipment_id;
You could have ambiguous columns so you should add the related table name for each column:
SELECT cust_order.order_id
, cust_order.order_date
, cust_order.order_source_id
, order_source.source_desc
, customer.first
, customer.last
FROM customer
INNER JOIN cust_order ON cust_order.cust_id = customer.cust_id
INNER JOIN order_source ON cust_order.order_source_id = order_source.order_source_id
And you should not use old (pre 1992) implicit join syntax based on where clause but use explicit join class.
(Same problem for the second query).

MySQL Inner join naming error?

http://sqlfiddle.com/#!9/e6effb/1
I'm trying to get a top 10 by revenue per brand for France on december.
There are 2 tables (first table has date, second table has brand and I'm trying to join them)
I get this error "FUNCTION db_9_d870e5.SUM does not exist. Check the 'Function Name Parsing and Resolution' section in the Reference Manual"
Is my use of Inner join there correct?
It's because you had an extra space after SUM. Please change it from
SUM (o1.total_net_revenue)to SUM(o1.total_net_revenue).
See more about it here.
Also after correcting it, your query still had more error as you were not selecting order_id on your intermediate table i2 so edited here as :
SELECT o1.order_id, o1.country, i2.brand,
SUM(o1.total_net_revenue)
FROM orders o1
INNER JOIN (
SELECT i1.brand, SUM(i1.net_revenue) AS total_net_revenue,order_id
FROM ordered_items i1
WHERE i1.country = 'France'
GROUP BY i1.brand
) i2
ON o1.order_id = i2.order_id AND o1.total_net_revenue = i2.total_net_revenue
AND o1.total_net_revenue = i2.total_net_revenue
WHERE o1.country = 'France' AND o1.created_at BETWEEN '2016-12-01' AND '2016-12-31'
GROUP BY 1,2,3
ORDER BY 4
LIMIT 10`
--EDIT stack Fan is correct that the o2.total_net_revenue exists. My confusion was because the data structure duplicated three columns between the tables, including one that was being looked for.
There were a couple errors with your SQL statement:
1. You were referencing an invalid column in your outer-select-SUM function. I believe you're actually after i2.total_net_revenue.
The table structure is terrible, the "important" columns (country, revenue, order_id) are duplicated between the two tables. I would also expect the revenue columns to share the same name, if they always have the same values in them. In the example, there's no difference between i1.net_revenue and o1.total_net_revenue.
In your inner join, you didn't reference i1.order_id, which meant that your "on" clause couldn't execute correctly.
PROTIP:
When you run into an issue like this, take all the complicated bits out of your query and get the base query working correctly first. THEN add your functions.
PROTIP:
In your GROUP BY clause, reference the actual columns, NOT the column numbers. It makes your query more robust.
This is the query I ended up with:
SELECT o1.order_id, o1.country, i2.brand,
SUM(i2.total_net_revenue) AS total_rev
FROM orders o1
INNER JOIN (
SELECT i1.order_id, i1.brand, SUM(i1.net_revenue) AS total_net_revenue
FROM ordered_items i1
WHERE i1.country = 'France'
GROUP BY i1.brand
) i2
ON o1.order_id = i2.order_id AND o1.total_net_revenue = i2.total_net_revenue
AND o1.total_net_revenue = i2.total_net_revenue
WHERE o1.country = 'France' AND o1.created_at BETWEEN '2016-12-01' AND '2016-12-31'
GROUP BY o1.order_id, o1.country, i2.brand
ORDER BY total_rev
LIMIT 10

Need mysql query to pull data from two tables

So after helpful feedback from my original question, I now have this query:
SELECT sessions.id, sessions.title, sessions.abstract, sessions.presenters, sessions.proposal_id, proposals.outcomes, proposals.CategorySelection, proposals.research3, proposals.research4, proposals.research5, proposals.research6, proposals.innovation3, proposals.innovation4, proposals.innovation5,proposals.innovation6, proposals.application3, proposals.application4, proposals.application5, proposals.application6, proposals.integration3, proposals.integration4, proposals.integration5, proposals.integration6, proposals.references, proposals.organization
FROM sessions, proposals
INNER JOIN proposals ON proposals.id = sessions.proposal_id
WHERE sessions.id = '$id
LIMIT 1;)
that is getting me nowhere fast. What am I doing wrong?
Original question:
I need to pull several fields from one table and several more from a second table. The criteria is that a field called proposal_id match the id field of the second table. I am fairly new so this is what I have so far. It is not working, but not sure how to make it work.
(SELECT `title`,`abstract`,`presenters`,`proposal_id` FROM `sessions` WHERE `id`='$id')
UNION
(SELECT `outcomes`,`CategorySelection`,`research3`,`research4`,`research5`,`research6`,`innovation3`,`innovation4`,`innovation5`,
`innovation6`,`application3`,`application4`,`application5`,`application6`,`integration3`,`integration4`,`integration5`,`integration6`,`references`,`organization` FROM `proposals` WHERE `id`= `sessions`.`proposal_id`)
LIMIT 1;
You need to use JOIN not UNION
select
s.*,p.*
from `sessions` s
inner join `proposals` p on p.id = s.proposal_id
where s.id = '$id'
This is how you can join both the tables using the common key between.
You can select the specific fields instead of .* by specifying the column names as
s.col1,s.col2,p.col1,p.col2
etc
Try to use JOINS, where you can match the related fields from both the tables , this is the most convenient way to fetch records from multiple tables
UNION is used when you want to combine two queries
select a.id,b.some_field from table1 as a
INNER JOIN table2 as b ON b.prospal_id = a.id

return results of same ID from 2 tables

I'm using an opensource database, so it's setup is a bit over my head.
Its basically like this.
A persons normal information is in the table 'person_per'
There is custom information in the table 'person_custom'
both use 'per_ID' to organize.
select per_ID from person_custom where c3 like '2';
gives my the IDs of people who fit my search, I want to "join" (I think) their name, phone, ect from the 'person_per' table using the ID as the "key"(terms I read that seem to fit).
How can I do that in a single query?
select per.*
from person_per per
inner join person_custom cus on cus.per_id = per.per_id
where cus.c3 = 2
You can retrieve all the columns from both tables with a single query:
SELECT p.name
, p.phone
, p.ect
, c.custom_col
FROM person_per p
JOIN person_custom c
ON c.per_ID = p.per_ID
WHERE c.c3 LIKE '2'
Use a JOIN operator between the table names, and include the "matching" criteria (predicate) in the ON clause.

How do I create a 3-way LEFT JOIN with MYSQL?

I'm attempting to create a 3-way LEFT JOIN using MYSQL and I'm having difficulty accomplishing it so I figured this would be the place to figure it out.
I have a three tables as I'll display below. The first contains a list of items that a user has added to their queue to be processed by the game. The other two contain details about each of the items such as their strengths, points to completion, etc. In the queued table, I have two types of items, units and research. The unit details are found in table 2 and the research details are found in table 3.
Table 1: The first table (core_queued_units) contains the following fields: id, unit_id, name, location, class(unit or research),sort.
Table 2: The second table (core_available_units) contains the following fields: id, name, description, etc.
Table 3: The third table (core_available_tech) contains the following fields: id, name, description, etc.
FROM core_queued_units
LEFT JOIN core_available_units
ON core_queued_units.unit_id = core_available_units.id
AND core_queued_units.class='Unit'
LEFT JOIN core_available_tech
ON core_queued_units.unit_id = core_available_tech.id
AND core_queued_units.class='Research'
WHERE core_queued_units.location = '1'
AND core_queued_units.user_id ='".$GLOBALS['self']['usrID']."'
ORDER BY core_queued_units.sort ASC
If fields in core_available_units and core_available_tech are equal, you can try this:
SELECT *
FROM core_queued_units cq
LEFT JOIN (
select cau.*, 'Unit' class from core_available_units cau
union all
select cat.*, 'Research' class from core_available_tech cat) c
ON cq.unit_id = c.unit_id and c.class = cq.class
WHERE cq.location = '1'
AND cq.user_id ='".$GLOBALS['self']['usrID']."'
ORDER BY cq.sort ASC
If you are trying to join multiple tables, you can do as follows. I am taking Table1 as base table.
SELECT columnname1, columnname2......
FROM Table1
LEFT OUTER JOIN Table2 ON Table1.PK = Table2.FK (here Table2.FK is the Primary key of Table1 which is used as Foreign key in Table2)
LEFT OUTER JOIN Table3 ON Table1.PK = Table3.FK (same condition as above)
WHERE insert your conditions here
using this sample, columns from Table2 & Table3 will added at the right most side of Table1.
Please inform if this helps.
Can you try to swap the arguments in the On-clause
core_queued_units.unit_id = core_available_tech.id?