I am working on opencart and fetching a list of products using below mentioned query and these products are displayed with most recent product on top.
$result = mysqli_query($con,"select p.image,p.price,p.store_name,p.deal_title_url,p.shop_now_url,p.coupon_code,p.special_deal_txt,p.special_comment_txt,s.price,d.name,p.date_added from oc_product p, oc_product_special s,oc_product_description d where p.product_id=s.product_id and p.product_id=d.product_id order by p.date_added DESC $limit;");
Note -
1. Limit is for pagenation
2. Data is fetched from multiple tables
Now i want to display all the featured products on top of the display. So basically the products which are in featured array must always be displayed on top and then remaining products based on the creation date.
The featured product id's are kept in an array - $featured_array
How can this be done?
Note that number of featured products may vary.
Try using UNION. In first query get featured products, in second put your current query. Limit is for union result
(
select p.image,p.price,p.store_name,p.deal_title_url,p.shop_now_url,p.coupon_code,p.special_deal_txt,p.special_comment_txt,s.price,d.name,p.date_added from oc_product p, oc_product_special s,oc_product_description d where p.product_id=s.product_id and p.product_id=d.product_id AND p.product_id IN($featuredProductsIds) order by p.date_added DESC
)
UNION
(
select p.image,p.price,p.store_name,p.deal_title_url,p.shop_now_url,p.coupon_code,p.special_deal_txt,p.special_comment_txt,s.price,d.name,p.date_added from oc_product p, oc_product_special s,oc_product_description d where p.product_id=s.product_id and p.product_id=d.product_id order by p.date_added DESC
)
$limit;
Related
I have only those rows that have multiple rows in the same table. For example see below image.
In the above picture you can see 2 highlighted columns one is for useid(u_id) and second is for product id (product_id).
So you can see user id of 7 has multiple product like (78,40,44,45,53) and user id 9 has multiple products like (79,75,79) same like user id 40 has multiple products.
so want out put like if particular user have multiple products then it will display 'multiple product' in product name column instead of display all product name.
above picture display all product but i need to display 'multiple product' message instead of all products if particular user have multiple products
I have used following query but not getting result that i want .
SELECT *
FROM orderlist
WHERE product_id IN (
SELECT product_id
FROM orderlist
GROUP BY product_id
HAVING COUNT(*) > 1
)
You could do the JOINS after aggregation
SELECT *,
CASE WHEN c.Counts > 1 THEN 'Multi' ELSE o.ProductName END ProductName
FROM
(
SELECT product_id, COUNT(*) Counts
FROM orderlist
GROUP BY product_id
)c INNER JOIN orderlist o ON o.product_id = c.product_id
I want to retrieve all products in side a subcategory . this is the code I've :
SELECT * from `wp_term_relationships` where term_taxonomy_id=$subcatId and
object_id in(select ID from `wp_posts` where `post_type`='product' and post_status='publish' and ID=wp_term_relationships.object_id)
the problem is , this code return about 20 products but when I go to that category in website ,it returns about 40 products.
could you help me ? I need a code to return a list of products inside a category .
SELECT * from `wp_term_relationships` where term_taxonomy_id=$subcatId and object_id in(select ID from `wp_posts` where `post_type`='product' and post_status='publish' and ID=wp_term_relationships.object_id) LIMIT 0,15000000
Use Limit keyword in your mysql query.
Limit accepts start and end value.
if you are giving Limit 5 it will display only top 5 records.
If you are giving Limit 5,10 it will display records between 5-10.
If you are giving Limit 0,big number (eg. Limit 0,100000000) it will display all the records upto 100000000.
Select all records using MySQL LIMIT and OFFSET query
I need to write an SQL query to list a customers prices for our products. We have a standard price list, customer_no = 0, and a customer specific price list, customer_no = XXXX.
I am having trouble understanding how I can get the query to return the customer specific price for a product, if they've been given such, or if not fall back to the standard price.
To get all products and prices on the standard price list
select prices.product_id, products.product_desc, prices.m2
from prices, products
where prices.product_id = products.product_id
and prices.customer_no = 0
order by prices.product_id asc
To get all products and prices that the customer has been specifically quoted for
select prices.product_id, products.product_desc, prices.m2
from prices, products
where prices.product_id = products.product_id
and prices.customer_no = $_SESSION['customer']
order by prices.product_id asc
How can I perform the first query, but if the customer has their own price then replace it with that? Is this even possible?
Thanks in advance.
Steve
Edit: Sorry, missed the third line in both queries in the original post.
You have to join with the prices table twice, once for the list prices and then for the quoted prices. Use a LEFT JOIN for the latter, since some products won't have a quoted price. Then use NVL to default from the quoted price to the list price.
SELECT products.product_id, products.product_desc, NVL(p2.m2, p1.m2)
FROM products
JOIN prices p1 ON p1.product_id = products.product_id
LEFT JOIN prices p2 ON p2.product_id = products.product_id
AND p2.customer_no = $_SESSION['customer']
WHERE p1.customer_no = 0
Try:
select prices.product_id, products.product_desc, prices.m2
from prices, products
where prices.customer_no = nvl($_SESSION['customer'], 0)
order by prices.product_id asc
I am creating a simple blog with categories in php.
I want that myblog.com/category.php?id=3 show me this:
TITLE of the category 3
// other stuff
ALL POSTS of the category 3
So, actually I do 2 queries ( 1 for getting the title and 1 for getting the posts ).
Is there a way to do this with 1 query ?
Depending on your database tables, you could something like that
SELECT c.title, p.data FROM category c LEFT JOIN post p ON p.category = c.category ORDER BY p.date
The category title will be repeated for every post though
If you need all fields from these tables so I think you should use #Damien's way.
If you need only titles you can use following query. In this query the first row is a title of a CATEGORY and next rows are titles of posts.
select * from
(
select 0 as ordField, categoris.Category_TITLE as Title from categoris where id =3
union all
select 1 as ordField, POSTS.Post_TITLE as Title from POSTS where category_id=3
) t order by ordField
I have several products each with several images.
I am trying to display 3 products with only 1 of their corresponding images.
With the code below i get 3 different products with only 1 image being successfully displayed.
If i take off the sub-query LIMIT 1 i get 3 of the same product just with different images.
Any help would be greatly appreciated.
SELECT pro.id,
pro.title AS product_title,
pro.price,
img.image,
img.title AS image_title,
FROM products AS pro
LEFT JOIN
(SELECT product_id,image_library_id
FROM images_products
LIMIT 1) AS ips
ON ips.product_id = pro.id
LEFT JOIN image_library AS img
ON img.id = ips.image_library_id
WHERE pro.status_id='1'
LIMIT 3
Your question-title mentions a "correlated subquery", but this actually isn't a correlated subquery; it's a regular (uncorrelated) subquery that you're joining to. So that subquery is performed first, before the join, and only returns one record from images_products at all. You'd therefore get at most one working image. I believe that the query you want is this:
SELECT pro.id,
pro.title AS product_title,
pro.price,
img.image,
img.title AS image_title
FROM products AS pro
LEFT
JOIN image_library AS img
ON img.id =
( SELECT image_library_id
FROM images_products AS ips
WHERE ips.product_id = pro.id
LIMIT 1
)
WHERE pro.status_id = '1'
LIMIT 3
;
using a true correlated subquery. (Tested.)