Joining Tables in MySQL Query instead of multiple queries - mysql

I have this query
$query = "SELECT * FROM items WHERE itemStatus = '1' AND itemAdded > '$timestamp'";
Once this query has returned results I loop through the results
the results array is itemID, itemLocationID, categoryParentID, categoryID, itemName, itemDetails
During the loop I then run three other queries by calling functions within the same class
$locationName = $this->getLocationByName($locationID);
$categoryParentName = $this->getCategoryByName($categoryParentID);
$categoryName = $this->getCategoryByName($categoryID);
the function getLocationByName performs this query;
$q = mysql_query("SELECT * FROM locations WHERE locationID = '$locationID'");
this returns an array of locationID, locationName, locationLink
the function getCategoryByName performs this query;
$q = mysql_query("SELECT * FROM categories WHERE categoryID = '$categoryID'");
this returns an array of categoryID, categoryName, categoryLink
Could someone please help me optimize this query and maybe join them to save doing so many queries.
thanks in advance.
Im now using this query
$q = mysql_query("SELECT
i.itemID,
i.locationID,
i.categoryParentID,
i.categoryID,
i.itemName,
i.itemDetails,
l.*,
c.*
FROM
items i
inner join locations l on i.locationID = l.locationID
inner join categories c on i.categoryID = c.categoryID
WHERE
itemStatus = '1'
AND itemAdded > '$timestamp'")or die(mysql_error());
and the result is
Array
(
[itemID] => 81300
[locationID] => 17
[categoryParentID] => 21
[categoryID] => 183
[itemName] => blah
[itemDetails] => blah
[locationName] => brilliant it pulls in the location as expected.
[locationLink] => blah
[categoryName] => brilliant it pulls in the category as expected.
[categoryLink] => blah
)
[categoryName] => //these are missing for categoryParentID
[categoryLink] => //these are missing for categoryParentID

I would not use * in the select statement
The Query with the joins could be
SELECT
i.itemID,
i.itemLocationID,
i.categoryParentID,
i.categoryID,
i.itemName,
i.itemDetails,
l.*,
c.*
FROM
items i
inner join locations l on i.itemLocationID = l.locationID
inner join categories c on i.categoryID = c.categoryID
WHERE
itemStatus = '1'
AND itemAdded > '$timestamp'
I hope it be useful for you.
Cheers!

I think should be something similar to below query. I do not see where are you using $categoryParentName .
Using your queries and data:
SELECT * FROM items WHERE itemStatus = '1' AND itemAdded > '$timestamp'
SELECT * FROM locations WHERE locationID = '$locationID'
SELECT * FROM categories WHERE categoryID = '$categoryID'
$locationName = $this->getLocationByName($locationID);
$categoryParentName = $this->getCategoryByName($categoryParentID);
$categoryName = $this->getCategoryByName($categoryID);
Please let me know if this returns expected result set. Hope this helps
SELECT
it.itemID, it.itemLocationID, it.categoryParentID, it.categoryID, it.itemName, it.itemDetails,
l.locationID, l.locationName, l.locationLink,
c.categoryID, c.categoryName, c.categoryLink
FROM items it
LEFT JOIN locations l ON l.locationID = it.itemLocationID
LEFT JOIN categories c ON c.categoryID = it.categoryID
WHERE
it.itemStatus = '1'
AND it.itemAdded > '$timestamp'
Update query using categoryParentID - i am not saying is efficient but you can test and optimize as needed.
One option is to update above query - not sure that will work - and for large result sets using OR is not efficient:
LEFT JOIN categories c ON (c.categoryID = it.categoryID OR c.categoryID = it.categoryParentID)
The other option that i see is to get 2 result sets (see below) - one for categId = categId and second for categId = categParentId and combine the result set in one big result set.
SELECT
t.itemID, t.itemLocationID, t.categoryParentID, t.categoryID, t.itemName, t.itemDetails,
l.locationID, l.locationName, l.locationLink,
t.categoryID, t.categoryName, t.categoryLink
FROM
(
SELECT
it.itemID, it.itemLocationID, it.categoryParentID, it.categoryID, it.itemName, it.itemDetails,
c.categoryID, c.categoryName, c.categoryLink
FROM items it
INNER JOIN categories c ON c.categoryID = it.categoryID
WHERE
it.itemStatus = '1'
AND it.itemAdded > '$timestamp'
UNION -- [ALL]
SELECT
it.itemID, it.itemLocationID, it.categoryParentID, it.categoryID, it.itemName, it.itemDetails,
c.categoryID, c.categoryName, c.categoryLink
FROM items it
INNER JOIN categories c ON c.categoryID = it.categoryParentID
WHERE
it.itemStatus = '1' AND
it.itemAdded > '$timestamp'
) AS t
LEFT JOIN locations l ON l.locationID = t.itemLocationID
Other idea - not tested and assuming that id are int - will have to cast as string / char. There are a few options how you can write this query - if you post a structure table and some dummy data i am sure that someone will create a demo / sqlfiddle.
SELECT
it.itemID, it.itemLocationID, it.categoryParentID, it.categoryID, it.itemName, it.itemDetails,
l.locationID, l.locationName, l.locationLink,
c.categoryID, c.categoryName, c.categoryLink
FROM items it
LEFT JOIN locations l ON l.locationID = it.itemLocationID
WHERE
it.itemStatus = '1'
AND it.itemAdded > '$timestamp'
AND c.category ID IN ( SELECT GROUP_CONCAT(categs) FROM (SELECT CONCAT(categoryID, ",", categoryParentID) AS categs FROM items ))

Unless I'm missing something obvious, I'd probably suggest something like this as a first start:
select *
from items i
join locations l
on i.location_id=l.location_id
join categories c
on i.category_id=c.category_id
where item_status='1'
and itemAdded > '$timestamp'

Related

SQL Get multiple values into one column separated by comma

Need some help on this SQL Query. Node is a table of products and each product has n images on table field_data_field_images. I need to select all of those image file names into one column separated by comma (as example "image1.jpg, image2.jpg" and so on). So first I need to get all of the image ID's from field_data_field_images and then get the file names itself from file_managed.
SELECT node.*,
file_managed.filename,
field_data_field_product_code.field_product_code_value,
FROM node
LEFT JOIN field_data_field_images ON node.nid = field_data_field_images.entity_id
LEFT JOIN file_managed ON field_data_field_images.field_images_fid = file_managed.fid
WHERE node.language = 'lt' AND node.type = 'products'
GROUP BY nid
You can use group_concat(). The following query also introduces table aliases so the query is easier to write and to read:
SELECT n.*,
GROUP_CONCAT(fm.filename) as filenames
FROM node n LEFT JOIN
field_data_field_images dfi
ON n.nid = dfi.entity_id LEFT JOIN
file_managed fm
ON dfi.field_images_fid = fm.fid
WHERE n.language = 'lt' AND n.type = 'products'
GROUP BY n.nid
IF MS-SQL follow below code:
DECLARE #TableVar TABLE
(
nid INT ,
images NVARCHAR(400)
);
INSERT INTO #TableVar
( nid ,
images
)
SELECT nid,images
FROM node
LEFT JOIN field_data_field_images
ON node.nid = field_data_field_images.entity_id
LEFT JOIN file_managed
ON field_data_field_images.field_images_fid = file_managed.fid
WHERE node.language = 'lt' AND node.type = 'products'
GROUP BY nid
SELECT DISTINCT
nid ,
STUFF(( SELECT ',' + images
FROM #TableVar AS T
WHERE T.nid = T2.nid
FOR
XML PATH('')
), 1, 1, '') AS images
FROM #TableVar T2
GROUP BY nid ,
images

How to Make This SQL Query More Efficient?

I'm not sure how to make the following SQL query more efficient. Right now, the query is taking 8 - 12 seconds on a pretty fast server, but that's not close to fast enough for a Website when users are trying to load a page with this code on it. It's looking through tables with many rows, for instance the "Post" table has 717,873 rows. Basically, the query lists all Posts related to what the user is following (newest to oldest).
Is there a way to make it faster by only getting the last 20 results total based on PostTimeOrder?
Any help would be much appreciated or insight on anything that can be done to improve this situation. Thank you.
Here's the full SQL query (lots of nesting):
SELECT DISTINCT p.Id, UNIX_TIMESTAMP(p.PostCreationTime) AS PostCreationTime, p.Content AS Content, p.Bu AS Bu, p.Se AS Se, UNIX_TIMESTAMP(p.PostCreationTime) AS PostTimeOrder
FROM Post p
WHERE (p.Id IN (SELECT pc.PostId
FROM PostCreator pc
WHERE (pc.UserId IN (SELECT uf.FollowedId
FROM UserFollowing uf
WHERE uf.FollowingId = '100')
OR pc.UserId = '100')
))
OR (p.Id IN (SELECT pum.PostId
FROM PostUserMentions pum
WHERE (pum.UserId IN (SELECT uf.FollowedId
FROM UserFollowing uf
WHERE uf.FollowingId = '100')
OR pum.UserId = '100')
))
OR (p.Id IN (SELECT ssp.PostId
FROM SStreamPost ssp
WHERE (ssp.SStreamId IN (SELECT ssf.SStreamId
FROM SStreamFollowing ssf
WHERE ssf.UserId = '100'))
))
OR (p.Id IN (SELECT psm.PostId
FROM PostSMentions psm
WHERE (psm.StockId IN (SELECT sf.StockId
FROM StockFollowing sf
WHERE sf.UserId = '100' ))
))
UNION ALL
SELECT DISTINCT p.Id AS Id, UNIX_TIMESTAMP(p.PostCreationTime) AS PostCreationTime, p.Content AS Content, p.Bu AS Bu, p.Se AS Se, UNIX_TIMESTAMP(upe.PostEchoTime) AS PostTimeOrder
FROM Post p
INNER JOIN UserPostE upe
on p.Id = upe.PostId
INNER JOIN UserFollowing uf
on (upe.UserId = uf.FollowedId AND (uf.FollowingId = '100' OR upe.UserId = '100'))
ORDER BY PostTimeOrder DESC;
Changing your p.ID in (...) predicates to existence predicates with correlated subqueries may help. Also since both halves of your union all query are pulling from the Post table and possibly returning nearly identical records you might be able to combine the two into one query by left outer joining to UserPostE and adding upe.PostID is not null as an OR condition in the WHERE clause. UserFollowing will still inner join to UPE. If you want the same Post record twice once with upe.PostEchoTime and once with p.PostCreationTime as the PostTimeOrder you'll need keep the UNION ALL
SELECT
DISTINCT -- <<=- May not be needed
p.Id
, UNIX_TIMESTAMP(p.PostCreationTime) AS PostCreationTime
, p.Content AS Content
, p.Bu AS Bu
, p.Se AS Se
, UNIX_TIMESTAMP(coalesce( upe.PostEchoTime
, p.PostCreationTime)) AS PostTimeOrder
FROM Post p
LEFT JOIN UserPostE upe
INNER JOIN UserFollowing uf
on (upe.UserId = uf.FollowedId AND
(uf.FollowingId = '100' OR
upe.UserId = '100'))
on p.Id = upe.PostId
WHERE upe.PostID is not null
or exists (SELECT 1
FROM PostCreator pc
WHERE pc.PostId = p.ID
and pc.UserId = '100'
or exists (SELECT 1
FROM UserFollowing uf
WHERE uf.FollowedId = pc.UserID
and uf.FollowingId = '100')
)
OR exists (SELECT 1
FROM PostUserMentions pum
WHERE pum.PostId = p.ID
and pum.UserId = '100'
or exists (SELECT 1
FROM UserFollowing uf
WHERE uf.FollowedId = pum.UserId
and uf.FollowingId = '100')
)
OR exists (SELECT 1
FROM SStreamPost ssp
WHERE ssp.PostId = p.ID
and exists (SELECT 1
FROM SStreamFollowing ssf
WHERE ssf.SStreamId = ssp.SStreamId
and ssf.UserId = '100')
)
OR exists (SELECT 1
FROM PostSMentions psm
WHERE psm.PostId = p.ID
and exists (SELECT
FROM StockFollowing sf
WHERE sf.StockId = psm.StockId
and sf.UserId = '100' )
)
ORDER BY PostTimeOrder DESC
The from section could alternatively be rewritten to also use an existence clause with a correlated sub query:
FROM Post p
LEFT JOIN UserPostE upe
on p.Id = upe.PostId
and ( upe.UserId = '100'
or exists (select 1
from UserFollowing uf
where uf.FollwedID = upe.UserID
and uf.FollowingId = '100'))
Turn IN ( SELECT ... ) into a JOIN .. ON ... (see below)
Turn OR into UNION (see below)
Some the tables are many:many mappings? Such as SStreamFollowing? Follow the tips in http://mysql.rjweb.org/doc.php/index_cookbook_mysql#many_to_many_mapping_table
Example of IN:
SELECT ssp.PostId
FROM SStreamPost ssp
WHERE (ssp.SStreamId IN (
SELECT ssf.SStreamId
FROM SStreamFollowing ssf
WHERE ssf.UserId = '100' ))
-->
SELECT ssp.PostId
FROM SStreamPost ssp
JOIN SStreamFollowing ssf ON ssp.SStreamId = ssf.SStreamId
WHERE ssf.UserId = '100'
The big WHERE with all the INs becomes something like
JOIN ( ( SELECT pc.PostId AS id ... )
UNION ( SELECT pum.PostId ... )
UNION ( SELECT ssp.PostId ... )
UNION ( SELECT psm.PostId ... ) )
Get what you can done of that those suggestions, then come back for more advice if you still need it. And bring SHOW CREATE TABLE with you.

Mysql REGEXP 2 words -- Same as AND LIKE

I have
SELECT p.*, gc.*, g.*, m.*, b.*
FROM (products AS p)
LEFT JOIN merchants AS m
ON m.merchantId = p.merchantId
LEFT JOIN brands AS b
ON b.brand = p.brand
LEFT JOIN groups_content gc
ON p.brand = gc.brandsSelect
LEFT JOIN groups g
ON g.group_id = gc.group_id
WHERE `p`.`percentOff` > gc.percent_off
AND `g`.`group_active` = 1
AND `p`.`price_sale` BETWEEN gc.price_min
AND gc.price_max
AND `gc`.`group_content_id` = '180'
AND `p`.`keyword` LIKE '%women%' AND `p`.`keyword` LIKE '%jacket%'
AND `p`.`status` = 1
ORDER BY p.price_sale ASC
this return me 158 results
Also I have
SELECT p.*, gc.*, g.*, m.*, b.*
FROM (products AS p)
LEFT JOIN merchants AS m
ON m.merchantId = p.merchantId
LEFT JOIN brands AS b
ON b.brand = p.brand
LEFT JOIN groups_content gc
ON p.brand = gc.brandsSelect
LEFT JOIN groups g
ON g.group_id = gc.group_id
WHERE `p`.`percentOff` > gc.percent_off
AND `g`.`group_active` = 1
AND `p`.`price_sale` BETWEEN gc.price_min
AND gc.price_max
AND `gc`.`group_content_id` = '180'
AND `p`.`keyword` REGEXP 'women.+jacket'
AND `p`.`status` = 1
ORDER BY p.price_sale ASC
This should return me same result like previous. But returns me just 17 results. What I am doing wrong?
----------
AFTER MANY SEARCHES here it is the right REGEXP
AND p.`keyword` REGEXP '^(.+jacket.+women.+)|^(.+women.+jacket.+).*$'
Your regex and like are not equivalent statements:
CREATE TABLE tab(keyword VARCHAR(100));
INSERT INTO tab(keyword)
VALUES ('jacket for women');
SELECT *
FROM tab p
WHERE `p`.`keyword` LIKE '%women%' AND `p`.`keyword` LIKE '%jacket%';
-- jacket for women
SELECT *
FROM tab p
WHERE p.`keyword` REGEXP 'women.+jacket';
-- (empty)
SqlFiddleDemo
"woman" anywhere in string and "jacket" anywhere in string
vs.
"woman" at the beginning, then any characters then "jacket" at the end
EDIT:
But How do I get -- "woman" anywhere in string and "jacket" anywhere
in string --- using regular expression?
One way is to use:
SELECT *
FROM tab
WHERE keyword REGEXP '.*women.*'
AND keyword REGEXP '.*jacket.*';
I am sure there exists one regex for this case.

MySQL search query with multiple joins and subqueries running slow

I have the following query which is actually within a stored procedure, but I removed it as there is too much going on inside the stored procedure. Basically this is the end result which takes ages (more than a minute) to run and I know the reason why - as you will also see from looking at the result of the explain - but I just cannot get it sorted.
Just to quickly explain what this query is doing. It is fetching all products from companies that are "connected" to the company where li.nToObjectID = 37. The result also returns some other information about the other companies like its name, company id, etc.
SELECT DISTINCT
SQL_CALC_FOUND_ROWS
p.id,
p.sTitle,
p.sTeaser,
p.TimeStamp,
p.ExpiryDate,
p.InStoreDate,
p.sCreator,
p.sProductCode,
p.nRetailPrice,
p.nCostPrice,
p.bPublic,
c.id as nCompanyID,
c.sName as sCompany,
m.id as nMID,
m.sFileName as sHighResFileName,
m.nSize,
(
Select sName
FROM tblBrand
WHERE id = p.nBrandID
) as sBrand,
(
Select t.sFileName
FROM tblThumbnail t
where t.nMediaID = m.id AND
t.sType = "thumbnail"
) as sFileName,
(
Select t.nWidth
FROM tblThumbnail t
where t.nMediaID = m.id AND
t.sType = "thumbnail"
) as nWidth,
(
Select t.nHeight
FROM tblThumbnail t
where t.nMediaID = m.id AND
t.sType = "thumbnail"
) as nHeight,
IF (
(
SELECT COUNT(id) FROM tblLink
WHERE
sType = "company"
AND sStatus = "active"
AND nToObjectID = 37
AND nFromObjectID = u.nCompanyID
),
1,
0
) AS bLinked
FROM tblProduct p
INNER JOIN tblMedia m
ON (
m.nTypeID = p.id AND
m.sType = "product"
)
INNER JOIN tblUser u
ON u.id = p.nUserID
INNER JOIN tblCompany c
ON u.nCompanyID = c.id
LEFT JOIN tblLink li
ON (
li.sType = "company"
AND li.sStatus = "active"
AND li.nToObjectID = 37
AND li.nFromObjectID = u.nCompanyID
)
WHERE c.bActive = 1
AND p.bArchive = 0
AND p.bActive = 1
AND NOW() <= p.ExpiryDate
AND (
li.id IS NOT NULL
OR (
li.id IS NULL
AND p.bPublic = 1
)
)
ORDER BY p.TimeStamp DESC
LIMIT 0, 52
Click here to see the output for EXPLAIN. Sorry, just couldn't get the formatting correct.
http://i60.tinypic.com/2hdqjgj.png
And lastly the number of rows for all the tables in this query:
tblProducts
Count: 5392
tblBrand
Count: 194
tblCompany
Count: 368
tblUser
Count: 416
tblMedia
Count: 5724
tblLink
Count: 24800
tblThumbnail
Count: 22207
So I have 2 questions:
1. Is there another way of writing this query which might potentially speed it up?
2. What index combination do I need for tblProducts so that not all the rows are searched through?
UPDATE 1
This is the new query after removing the subqueries and making use of left joins instead:
SELECT DISTINCT DISTINCT
SQL_CALC_FOUND_ROWS
p.id,
p.sTitle,
p.sTeaser,
p.TimeStamp,
p.ExpiryDate,
p.InStoreDate,
p.sCreator,
p.sProductCode,
p.nRetailPrice,
p.nCostPrice,
p.bPublic,
c.id as nCompanyID,
c.sName as sCompany,
m.id as nMID,
m.sFileName as sHighResFileName,
m.nSize,
brand.sName as sBrand,
thumb.sFilename,
thumb.nWidth,
thumb.nHeight,
IF (
(
SELECT COUNT(id) FROM tblLink
WHERE
sType = "company"
AND sStatus = "active"
AND nToObjectID = 37
AND nFromObjectID = u.nCompanyID
),
1,
0
) AS bLinked
FROM tblProduct p
INNER JOIN tblMedia m
ON (
m.nTypeID = p.id AND
m.sType = "product"
)
INNER JOIN tblUser u
ON u.id = p.nUserID
INNER JOIN tblCompany c
ON u.nCompanyID = c.id
LEFT JOIN tblLink li
ON (
li.sType = "company"
AND li.sStatus = "active"
AND li.nToObjectID = 37
AND li.nFromObjectID = u.nCompanyID
)
LEFT JOIN tblBrand AS brand
ON brand.id = p.nBrandID
LEFT JOIN tblThumbnail AS thumb
ON (
thumb.nMediaID = m.id
AND thumb.sType = 'thumbnail'
)
WHERE c.bActive = 1
AND p.bArchive = 0
AND p.bActive = 1
AND NOW() <= p.ExpiryDate
AND (
li.id IS NOT NULL
OR (
li.id IS NULL
AND p.bPublic = 1
)
)
ORDER BY p.TimeStamp DESC
LIMIT 0, 52;
UPDATE 2
ALTER TABLE tblThumbnail ADD INDEX (nMediaID,sType) USING BTREE;
ALTER TABLE tblMedia ADD INDEX (nTypeID,sType) USING BTREE;
ALTER TABLE tblProduct ADD INDEX (bArchive,bActive,ExpiryDate,bPublic,TimeStamp) USING BTREE;
After doing the above changes the explain showed that it is now only searching through 1464 rows on tblProduct instead of 5392.
That's a big query with a lot going on. It's going to take a few steps of work to optimize it. I will take the liberty of just presenting a couple of steps.
First step. Can you get rid of SQL_CALC_FOUND_ROWS and still have your program work correctly? If so, do that. When you specify SQL_CALC_FOUND_ROWS it sometimes means the server has to delay sending you the first row of your resultset until the last row is available.
Second step. Refactor the dependent subqueries to be JOINs instead.
Here's how you might approach that. Part of your query looks like this...
SELECT DISTINCT SQL_CALC_FOUND_ROWS
p.id,
...
c.id as nCompanyID,
...
m.id as nMID,
...
( /* dependent subquery to be removed */
Select sName
FROM tblBrand
WHERE id = p.nBrandID
) as sBrand,
( /* dependent subquery to be removed */
Select t.sFileName
FROM tblThumbnail t
where t.nMediaID = m.id AND
t.sType = "thumbnail"
) as sFileName,
( /* dependent subquery to be removed */
Select t.nWidth
FROM tblThumbnail t
where t.nMediaID = m.id AND
t.sType = "thumbnail"
) as nWidth,
( /* dependent subquery to be removed */
Select t.nHeight
FROM tblThumbnail t
where t.nMediaID = m.id AND
t.sType = "thumbnail"
) as nHeight,
...
Try this instead. Notice how the brand and thumbnail dependent subqueries disappear. You had three dependent subqueries for the thumbnail; they can disappear into a single JOIN.
SELECT DISTINCT SQL_CALC_FOUND_ROWS
p.id,
...
brand.sName,
thumb.sFilename,
thumb.nWidth,
thumb.nHeight,
...
FROM tblProduct p
INNER JOIN tblMedia AS m ON (m.nTypeID = p.id AND m.sType = 'product')
... (other table joins) ...
LEFT JOIN tblBrand AS brand ON p.id = p.nBrandID
LEFT JOIN tblMedia AS thumb ON (t.nMediaID = m.id AND thumb.sType = 'thumbnail')
I used LEFT JOIN rather than INNER JOIN so MySQL will present NULL values if the joined rows are missing.
Edit
You're using a join pattern that looks like this:
JOIN sometable AS s ON (s.someID = m.id AND s.sType = 'string')
You seem to do this for a few tables. You probably can speed up the JOIN operations by creating compound indexes in those tables. For example, try adding the following index to tblThumbnail: (sType, nMediaID). You can do that with this DDL statement.
ALTER TABLE tblThumbnail ADD INDEX (sType, nMediaID) USING BTREE
You can do similar things to other tables with the same join pattern.

Sql query in Microsoft Access

I have this query written in Microsoft Access:
SELECT p.artnbr AS [Number],
p.name AS Name,
s.sizename AS Sizes,
s.sizeindex AS SizeIndex,
s.oid AS SizeId,
l.name AS LocationName,
(SELECT od.quantity
FROM orderdetails od
WHERE od.ORDER = (SELECT o.oid
FROM [order] o
WHERE o.active = -1
AND o.location = l.oid)
AND od.productsize = s.oid) AS Quantity
FROM [size] AS s
INNER JOIN (product AS p
INNER JOIN (favorite AS f
INNER JOIN location AS l
ON f.customer = l.customer)
ON p.oid = f.product)
ON p.oid = s.product
WHERE f.customer = #customer
The link below shows tables i used and their relationships.
https://dl.dropbox.com/u/18377860/QueryTables.png
This query returns a correct result, but as u can see i am using a Sub query to get the quantity. I cant figure out how can i rewrite this query using join statements instead of that long sub-query. Any help would be appreciated.
Regards
EDIT: To make it more clear, my query needs to get all unique combinations of (product, size, location) whether they have an [Order] or not and display ordered quantity. If an order does not exist for a particular combination the quantity should be null.
EDIT2:
I managed to build the query and it looks like this:
SELECT p.ArtNbr AS [Number],
p.Name AS Name,
s.SizeName AS Sizes,
s.SizeIndex AS SizeIndex,
s.Oid AS SizeId,
l.Name AS LocationName,
so.qty AS Quantity
FROM ([Size] AS s
INNER JOIN (Product AS p
INNER JOIN (Favorite AS f
INNER JOIN Location AS l
ON f.Customer = l.Customer) ON p.OID = f.Product)
ON p.OID = s.Product)
LEFT JOIN
(SELECT od.ProductSize AS PS,
od.Quantity AS qty,
o.Location as Location
FROM OrderDetails AS od INNER JOIN [Order] AS o ON od.Order = o.OID
WHERE o.Active = -1) AS so ON so.PS = s.OID
WHERE f.Customer = #customer AND (l.OID = so.Location OR so.Location is null)
I did like Matt said, i moved the sub-query into the FROM section, i included ProductSize and Location in the select query of the derived table, then linking the result with "Size" table and i finally added a condition to the where section to eliminate the repetition of the same record for each Location.
I managed to build the query and it looks like this:
SELECT p.ArtNbr AS [Number],
p.Name AS Name,
s.SizeName AS Sizes,
s.SizeIndex AS SizeIndex,
s.Oid AS SizeId,
l.Name AS LocationName,
so.qty AS Quantity
FROM ([Size] AS s
INNER JOIN (Product AS p
INNER JOIN (Favorite AS f
INNER JOIN Location AS l
ON f.Customer = l.Customer) ON p.OID = f.Product)
ON p.OID = s.Product)
LEFT JOIN
(SELECT od.ProductSize AS PS,
od.Quantity AS qty,
o.Location as Location
FROM OrderDetails AS od INNER JOIN [Order] AS o ON od.Order = o.OID
WHERE o.Active = -1) AS so ON so.PS = s.OID
WHERE f.Customer = #customer AND (l.OID = so.Location OR so.Location is null)
I did like Matt said, i moved the sub-query into the FROM section, i included ProductSize and Location in the select query of the derived table, then linking the result with "Size" table and i finally added a condition to the where section to eliminate the repetition of the same record for each Location.