SQL join not working - mysql

I am trying to merge these two queries I have on a join but its not working out so well can you tell me where i'm going wrong. Thanks in advance
SELECT z1blto,
z1ctrk
FROM z1mast AS e
JOIN d
(
select a.ordprod,
a.ordtotpcs ,
a.ordordnum,
b.orhdate
FROM dta/dw30d a
JOIN dta/dw30c b
ON a.ordcust = b.orhcust
AND a.ordordnum = b.orhordnum
WHERE a.ordcust = 'GL02'
AND a.ordprod = '2002534B_GC'
ORDER BY b.orhdate) AS d
ON e.z1cucd = d.ordcust

Looks like you have a typo in your query. Your ON specification uses ordcust for a and orhcust for b, similarly for ordornum and orddate. Here is the corrected version. And you have an extra d before the nested query. I'm making some assumptions regarding spelling, as the information is missing from your questions.
SELECT z1blto,
z1ctrk
FROM z1mast AS e
JOIN
(
select a.ordprod,
a.ordtotpcs ,
a.ordordnum,
b.orddate
FROM dta/dw30d a
JOIN dta/dw30c b
ON a.ordcust = b.ordcust
AND a.ordordnum = b.ordordnum
WHERE a.ordcust = 'GL02'
AND a.ordprod = '2002534B_GC'
ORDER BY b.orddate) AS d
ON e.z1cucd = d.ordcust

Related

How will I join these tables?

I've got a problem joining these tables; I have this code.
my query :
SELECT partial a.{ediTransactionDetailId, poNumber},
partial b.{edi997DetailId, noOfTrans},
partial c.{ediTransactionId, senderId, receiverId, gsNumber, isaNumber, fileName},
partial d.{ediDocTypeId, docType}
FROM
MATRIXEDIBUNDLE:editransactiondetail a
JOIN a.edi997details b
JOIN b.editransaction c
JOIN c.edidoctype d
WHERE c.filename LIKE :fileName
AND a.ponumber LIKE :poNumber
AND d.doctype = :docType
AND a.flag = 1
AND c.flag = 1
and I got this error :
JOIN b.ediTransaction': Error: Class
Matrix\MatrixEdiBundle\Entity\EdiTransactionDetail has no association
named edi997Details
How can I join it?
You have not made relationship among tables, that's why you got the error here,
SELECT table1.column1, table2.column2...
FROM table1 JOIN table2
ON table1.common_field = table2.common_field; // (This part is missing in your code)
For further study http://www.tutorialspoint.com/sql/sql-using-joins.htm
Your code should look something like this,
SELECT a.edi_transaction_id, a.sender_id, a.receiver_id, a.gs_number, a.isa_number, a.file_name,
b.edi_997_detail_id, b.no_of_trans,
c.edi_transaction_etail_id, c.po_number,
d.edi_doc_type_id, d.doc_type
FROM (((edi_transaction a
left join edi_997_details b on a.edi_transaction_id = b.edi_transaction_id)
left join edi_transaction_details c on a.edi_transaction_id = c.edi_transaction_id)
left join edi_doc_type d on a.edi_doc_type_id = d.edi_doc_type_id)
WHERE a.file_name like '%Your file Name%'
and c.po_number like '%Your file Name%'
and d.doc_type = 'your doc type'
and a.flag = 1 AND c.flag = 1;
Hope this will accomplish your task.
Your need to learn JOIN sintaxis
either you need the ON clausule
FROM MatrixEdiBundle:EdiTransactionDetail a
JOIN Details b
ON a.SomeID = B.SomeID
Or you need a CROSS JOIN
FROM MatrixEdiBundle:EdiTransactionDetail a
CROSS JOIN Details b

MySQL statement, JOIN

I have these tables :
I don't know how I can write a statement, that takes emails from Table "Firm", that have Location_id = '1' and Category_id = '130';
I know that I should use JOINs, but I'm not sure how to go from there.
SELECT Firm.email
FROM Firm
INNER JOIN FirmID ON Firm.firma_id = FirmID.firma_id
WHERE FirmID.location_id = '1'
AND FirmID.Category_id = '130'
Should be as simple as doing the following:
SELECT email
FROM Firm, FirmID
WHERE Firm.firma_id = FirmID.firma_id
AND FirmID.location_id = 1
AND FirmID.category_id = 130;
It does a join behind the scenes, but can be a bit clearer to understand than using the JOIN keyword.
You could do:
SELECT f.email
FROM Firm f
WHERE f.firma_id =
(
SELECT ff.firma_id
FROM FirmID ff
WHERE ff.location_id = 1
AND ff.category_id = 130
)
Using an inner select.
But using JOINS is in the long term the way to go, what have you tried and what's not working?

Need help updating table from a select query

I have the following code that produces a list of order numbers and values...
SELECT
d.`OrderNo`,
SUM(v.`UnitPrice`)
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.`VMainID` = d.`MainID`
GROUP BY d.`OrderNo`;
I need to update a table called matcontctsafter which has an OrderNo field and currently blank InvoiceAmount column that I need the relative SUM(v.UnitPrice) in.
Can anybody help me construct the UPDATE clause?
UPDATE matcontctsafter m
INNER JOIN (
SELECT
d.`OrderNo`,
SUM(v.`UnitPrice`) InvoiceAmount
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.`VMainID` = d.`MainID`
GROUP BY d.`OrderNo`
) sq ON m.OrderNo = sq.OrderNo
SET m.InvoiceAmount = sq.InvoiceAmount;
UPDATE matcontctsafter m SET m.InvoiceAmount = (SELECT
SUM(v.UnitPrice)
FROM tblverification v
LEFT JOIN tblorderdetailsafter d ON v.VMainID = d.MainID
WHERE m.OrderNo = d.OrderNo);

Struggling with MySQL query

I have the following model:
I've been at this for a while and still don't know how to tackle it properly. Already looked at joining two aliased subqueries, joining two views, and tried a gynormous and ugly all-in one query, none of which worked.
My question is simple:
How can I select deivce_names.name and match them to a model and manufacturer?
SELECT
name, manufacturer, model
FROM
device_names
JOIN devices ON (device_names.id = name_id)
JOIN devices_generic ON (generic_device_id = devices_generic.id)
JOIN device_manufacturers ON (manufacturer_id = device_manufacturers.id)
JOIN device_models ON (model_id = device_models.id)
select
device_names.name as device_name,
device_manufacturers.name as device_manufacturer_name,
device_models.name as device_model_name
from deivce_names
join device on device.name_id = deivce_names.id
join devices_generic on devices_generic.id = devices.generic_device_id
join device_manufacturers on devices_generic.manufacturer_id = device_manufacturers.id
join device_models on device_models.id = devices_generic.model_id
SELECT dm1.manufacturer, dm2.model from device_manufacturers as dm1 join devices_generic as dg on dg.manufacturer_id = dm1.id join device_models as dm2 on dm2.id = dg.model_id join devices as d2 on d2.generic_device_id = dg.id join device_names as dn on dn.id = d2.name_id where name = 'foo'
In the future, you might want to simply put your data into a fiddle and play with it there.

Left joins in LINQ to SQL

This is my query:
Dim bugs = (From b In bugCon.bugs Where sctUserIds.Contains(b.Developer.Value) Order By b.bg_id Select Bug = b, Project = b.project).ToList
Currently this does an inner join between "bugs" and "projects". How do I turn it into a left join?
I haven't tested this, but the query below should get you headed in the right direction. The key is the join ... into syntax and the use of DefaultIfEmpty()
from b in context.Bugs
join p in context.Projects
on b.projectID equals p.projectID into BugProjects
where sctUserIds.Contains(b.Developer.Value)
from bugProjects in BugProjects.DefaultIfEmpty()
select new {
Name = p.Name,
...
BugProjects = bugProjects
}