I have four tables, products, priceplans, and two category tables as follows:
products
---------
product_id
a
b
priceplans
---------
priceplan_id
a
b
product_id (can be null)
price
categoryA
---------
a (id)
category_name
categoryB
---------
b (id)
category_name
In the priceplans a and b are category ids in other tables, the combination (a,b,product_id) is unique but product_id can also be null, and the priceplan should then use the general priceplan for the (a,b,null) combination. That is the theory, but it is not working out as well as I had hoped and I havent managed to construct a query to only filter them out.
Example:
products - 3 products, 2 in the same category, one in another category
product_id a b
1 1 1
2 1 1
3 1 2
priceplans - 3 plans,
1 is for the default (a,b)=(1,1) category combination when there is no product_id,
2 is supposed to override the default as we have declared a product_id, and
3 is the default for (a,b)=(1,2) combination
priceplan_id a b product_id price
1 1 1 null 10
2 1 1 2 15
3 1 2 null 12
What I want the outcome to look like when I join products with the priceplans is:
product_id a b priceplan_id price
1 1 1 1 10
2 1 1 2 15
3 1 2 3 12
If for the product with a category combination (a,b)=(1,1) and id=1 i want the priceplan with combination (a,b,1) if it exists, if not i want the (a,b,null) priceplan. Any suggestions?
I finally managed to create the query I was looking for:
select pro.*, pp.* from products pro
left join price_plans pp
on pro.a=pp.a
and pro.b=pp.b
and (pro.product_id=pp.product_id or pp.product_id is null)
left join price_plans pp2
on pro.a=pp2.a
and pro.b=pp2.b
and pp2.product_id =pro.product_id
where pp.product_id <=> pp2.product_id
Related
I have 3 tables as such
Product
ProductID
ProductDetails
1
...
2
...
3
...
Vendor
VendorID
VendorDetails
1
...
2
...
3
...
ProductVendors
ProductID
VendorID
1
1
2
1
1
2
2
2
3
2
How would I go about finding the number of products that are not mapped to a specific vendor.
I tried:
SELECT
COUNT(pr.id) AS product_count
FROM
products pr
LEFT JOIN vendor_product_map vp ON
pr.id = vp.product
LEFT JOIN vendors vv ON
vp.vendor = vv.id
WHERE
vv.id = 3 AND vp.vendor IS NULL
but that doesn't seem right. Any help is appreciated
A simple not exists query should be sufficient:
select *
from products
where not exists (
select *
from vendor_product_map
where vendor_product_map.product = product.id
and vendor_product_map.vendor = 12345678
)
My code
$result=DB::table('receipts')
->join('dealer_m','receipts.ToDealerID','dealer_m.DealerID')
->join('dealer_m','receipts.FromDealerID','dealer_m.DealerID')
->join('product_m','receipts.ProductID','product_m.ProductID')->get();
table receipts
Id | FromDealerId | ToDealerId | ProductId
---+---------------+-------------+------------
1 1 2 1
2 1 3 1
3 3 1 1
Table dealer_m
DealerId | DealerName
---------+-----------
1 Dealer One
2 Dealer Two
3 Dealer Three
Table product_m
ProductId | ProductName
----------+-----------
1 Product One
2 Product Two
Expected Output
Id | FromDealerId | ToDealerId | ProductId | FromDealerName | ToDealerName | ProductName
---+---------------+-------------+------------+----------------+--------------+------------
1 1 2 1 Dealer One Dealer Two Product One
2 1 3 1 Dealer One Dealer Three Product One
3 3 1 1 Dealer Three Dealer One Product One
When I run my code it gets an error
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique
table/alias: 'dealer_m' (SQL: select '' from 'receipts' inner join
'dealer_m' on 'receipts'.'ToDealerID' = `dealer_m'.'DealerID' inner
join 'dealer_m' on 'receipts'.'FromDealerID' = 'dealer_m'.'DealerID'
inner join 'product_m' on 'receipts'.'ProductID' =
'product_m'.'ProductID')
How to solve this ???
Thank You
Try This:
$results=DB::table('receipts')
->select('receipts.*','dealer_m1.DealerName as ToDealer','dealer_m2.DealerName as FromDealer','product_m.ProductName')
->join('dealer_m as dealer1','receipts.ToDealerID','dealer_m.DealerID')
->join('dealer_m as dealer2','receipts.FromDealerID','dealer_m.DealerID')
->join('product_m','receipts.ProductID','product_m.ProductID')->get();
You just need to give each joined table an alias
Now in your blade:
#foreach ($results as $result)
<tr>
<td>{{$result->Id}}</td>
<td>{{$result->FromDealerId}}</td>
<td>{{$result->ToDealerId}}</td>
<td>{{$result->ProductId}}</td>
<td>{{$result->FromDealer}}</td>
<td>{{$result->ToDealer}}</td>
<td>{{$result->ProductName}}</td>
</tr>
#endforeach
Of course if you ever get stuck on what to call in your blade, just print our the collection to find the key => values or do dd($results); in the controller to inspect it before it gets to the view.
You should try this:
$result=DB::table('receipts')
->join('dealer_m as dealer_id','receipts.ToDealerID','dealer_m.DealerID')
->join('dealer_m as dealer_f_id','receipts.FromDealerID','dealer_m.DealerID')
->join('dealer_m as product_p_id','receipts.ProductID','product_m.ProductID')->get();
I need a quick way to find the Number of items in a table. The items are linked to an other table. Table 1 is products and table 2 is orders.
Orders contains a paid status (1 or 0).
Orders table example:
id paid
1 0
2 1
Products table example:
id orderid type
1 1 5
2 1 5
3 1 3
4 2 5
5 2 5
6 2 3
Products contains a id (orderid) that refers to the order and a type. So i need the number of products where type = 5 and paid = 1 in the orders table.
What is the best and fastest way to archieve this?
So I need all the paid products with type 5. The result should be '2'.
you can use join like this,
SELECT COUNT(*) AS num_rows
FROM products
LEFT JOIN orders ON orders.id = products.orderid
WHERE type = 5 AND paid = 1
One way is to use a join statement. Making some assumptions about your schema, the following should work:
SELECT COUNT(p.`id`) FROM `products_table` p
LEFT JOIN `orders_table` o ON p.`orderid` = o.`id`
WHERE o.`paid` = 1
AND p.`type` = 5
I have three tables that I can't change the structure of:
facet
id name
-----------------
1 Series
2 Material
value
id facet_id name
----------------------------------
1 2 Glass
2 2 Metal
3 1 Series #1
4 1 Series #2
5 1 Series #3
product_facet_values
product_id value_id
-----------------------------------
1 1
1 3
2 1
2 4
3 2
3 5
4 1
I am trying to write two queries:
/1. One that will return the ids that represent the series facet from the values table where a product record is Glass and has any series. So an output like this:
id facet_id name
----------------------------------
3 1 Series #1
4 1 Series #2
Record 1 is not a series.
Record 2 is not a series.
Record 3 is returned because product #1 has both a series and material and the material is glass.
Record 4 is returned because product #2 has both a series and material and the material is glass.
Record 5 is not returned because product #3 has the material of metal even though it has both a series and material.
/2. Same as number one but return a list of product ids.
product_id
---------------
1
2
Product #1 is returned because it has both a series and material and the material is glass.
Product #2 is returned because it has both a series and material and the material is glass.
Product #3 is not returned because it has the material of metal even though it has both a series and material set.
Product #4 is not returned because it has no series set even though the material is glass.
FIRST QUERY:
try this.. I believe this will do the trick.
SELECT
v.id
FROM value v
JOIN product_facet_values pfv ON pfv.value_id = v.id
WHERE pfv.product_id IN
( SELECT
product_id
FROM product_facet_values
WHERE
product_id IN
( SELECT
product_id
FROM product_facet_values
GROUP BY product_id
HAVING COUNT(*) > 1
)
AND value_id = 1
)
AND v.facet_id = 1;
SECOND QUERY:
the inner part of the same query returns the products that have a value of 1 and a value of something other than one so it would be this
SELECT
product_id
FROM product_facet_values
WHERE
product_id IN
( SELECT
product_id
FROM product_facet_values
GROUP BY product_id
HAVING COUNT(*) > 1
)
AND value_id = 1
EXPLANATION:
INNERMOST SUBQUERY:
SELECT
product_id
FROM product_facet_values
GROUP BY product_id
HAVING COUNT(*) > 1
give me products that have more than one record per product (later to be filtered by 'Glass' and 'Series')
MIDDLE SUBQUERY:
SELECT
product_id
FROM product_facet_values
WHERE
product_id IN
(
INNERMOST SUBQUERY
)
AND value_id = 1
give me products that have the value_id = 1 (aka 'Glass') that have more than one record.
OUTERMOST QUERY:
SELECT
v.id
FROM value v
JOIN product_facet_values pfv ON pfv.value_id = v.id
WHERE pfv.product_id IN
(
MIDDLE SUBQUERY
)
AND v.facet_id = 1;
give me the value id for the products that are related to 'Glass' but whos facet_id = 1 (aka series)
I have three tables:
item
id name etc
--------------------
1 Rex
2 Fido
3 Geoff
category
id name
------------
1 Dogs
2 Humans
3 Mammals
category_item
category_id item_id
--------------------
1 1
3 1
1 2
3 2
2 3
3 3
I also have an array of category ids. I would like to count the number of items that are related to ALL of the categories in the array.
For example...
Category_ids Result
----------------------
1,2 0
2,3 1
1,2,3 0
Pretty sure I'm gonna kick myself when I figure this one out.
Please try query given below..
select count(*) AS Result from (
SELECT count(item_id) FROM category_item
WHERE
category_id in (2 ,3)
GROUP by item_id
HAVING count(*) = 2
) AS temp
In this query put count(*) value equal to total number of category_id for example if you are checking for category_ids 1,2,3 then put having count(*) = 3. In this query let assume you provide category_id 1 and 2 then it will fetch total number of existence of item_id with
I hope this query will helpful for you.
thanks