Can't able to retrieve the data using joins - php

I've two tables level & product, basically I need to find out level.id = 1, but I also need to know if it has product attached to this id
in product table. There is a pdcatid in my product table. Now
the query works only if this id also in product table,
if product table doesn't have this id, it will fail and retrurn empty. How can I show both situations?
if has product, show pdcatid
if no product, show NULL
Here is my query I've tried
SELECT *
FROM level
RIGHT JOIN product on level.`id` = product.`pdcatid`
WHERE level.`id` = 1
Thank You.

Use left join instead of right join.

Try this:
SELECT * FROM level LEFT JOIN product on level.`id` = product.`pdcatid` WHERE level.`id` = 1

Related

Create a custom mysql JOIN query in Zen Cart

I am trying to the product's quantity and what attribute name was selected by the user. For example, what attribute name was chosen for product id = 180 where the order id = 2118 ?
any suggestion how I can right mysql join query for retrieve order data.
The way to handle problems like this is to break the query down into steps.
In Zen Cart, the attributes details for an order are stored in the table orders_products_attributes. (NOTE: I am assuming your tables have the prefix zen_. If they don't, just remove this from the queries below.) So in phpMyAdmin, you could use:
SELECT * FROMzen_orders_products_attributeswhere orders_id = 2118
This gives you all attributes but instead of a product id, you get an orders_products_id. So to map that back to a product_id, check the orders_products table.
SELECT * FROMzen_orders_productswhere orders_id = 2118
So now you can see that both tables contain orders_products_id and orders_id.
So use those two fields in your query. I'm going to guess that the fields products_options and products_options_values have the information you need.
SELECT products_options, products_options_values FROM zen_orders_products op, zen_orders_products_attributes opa WHERE op.orders_id = opa.orders_id AND op.orders_id = 2118 AND op.products_id=180;

Count how many times product is liked and all the products a particular user has liked in PHP and SQL

I am new to PHP and SQL. I am trying to count how many times product is liked and all the products a particular user has liked.But couldn't get the desired results.
I have 3 tables.
product table:
pro_id pro_info pro_price
user table:
id username password
and pro_likes table:
id user product
In pro_likes table "user" and "product" columns are foreign keys referring to user(id) and product(pro_id) respectively. How can I find out how many times a particular product has been liked and which product a particular user has liked?
I tried the following to find out the former but it return the counts for all the products, not a single one.
$q="SELECT
COUNT(pro_likes.product) AS likes
FROM pro_likes
LEFT JOINT products
ON pro_likes.product = products.pro_id";
$r=mysqli_query($con,$q);
$r1=mysqli_fetch_assoc($r);
echo $r1['likes'];
Can anyone please help?
You need to group your query by pro_likes.product
You also need to grab the product name if you're going to get unique rows, otherwise all you're doing is counting the counts of the join.
SELECT
COUNT(pro_likes.product) AS likes, pro_likes.product as product
FROM pro_likes
LEFT JOINT products
ON pro_likes.product = products.pro_id
GROUP BY pro_likes.product;
SELECT DISTINCT
COUNT([pro_likes.prodcuts]) AS likes,
pro_likes.product AS product
FROM pro_likes
LEFT JOIN products
ON pro_likes.product = products.pro_id
GROUP BY pro_likes.product
thank you all for your help.
I was able to accomplish this with the following code:
select count(pro_likes.product) as likes from pro_likes join products on pro_likes.product=products.pro_id where pro_likes.product='$pro_id'"
where $pro_id is products.pro_id that has been passed through URL from the previous page.

Query Between two date

SELECT product.pname,stock.pid,stock.qty,stock.rate
FROM product,stock
WHERE (date BETWEEN '2012-04-10' AND '2012-07-16') AND product.pid=stock.pid
This is my sql query but problem is when execute this query its show single result means my product table contains pid and pname and stock table contains pid,rate,qty and date.
I want to display record between two dates.
My query match with two records. But when i add "AND product.pid=stock.pid" its show only 1 record.
I want to display the product name from product table in the respect of pid of stock table.
Probably some product is not in the stock. Also I recommend you to do a JOIN like this:
SELECT product.pname,stock.pid,stock.qty,stock.rate
FROM product
LEFT JOIN
stock
ON product.pid=stock.pid
WHERE (date BETWEEN '2012-04-10' AND '2012-07-16')
I used LEFT JOIN that means to return products no matter if have or nor stock, I think that with this query you will get two Rows.
First try to get it working without the join:
SELECT stock.pid, stock.qty, stock.rate
FROM stock
WHERE stock.date BETWEEN '2012-04-10' AND '2012-07-16'
If that works, then use an outer to join to add information to each row without removing any rows.
SELECT product.pname, stock.pid, stock.qty, stock.rate
FROM stock
LEFT JOIN product ON product.pid=stock.pid
WHERE stock.date BETWEEN '2012-04-10' AND '2012-07-16'
The product name will be NULL if no product is found.
Try the below query.
SELECT product.pname,stock.pid,stock.qty,stock.rate
FROM product,stock
WHERE stock.date BETWEEN '2012-04-10' AND '2012-07-16' AND product.pid=stock.pid
Your selection criteria
WHERE ([stock.]date BETWEEN '2012-04-10' AND '2012-07-16')
makes it effectively RIGHT JOIN, only records that have/had stock within date range will be displayed. If you want to display ALL products irrespective of stock than you will need to add OR [stock.]date IS NULL to your selection criteria.

mysql select query to prepopulate dropdown fields

As part of my ecommerce application, I have this interesting problem, I've been trying to resolve.
I have two things.
Category
Product
In relational database sense, a category can have more than one products, and a product can only belong to category.
Then, I have this ecommerce admin coupon page where I want to modify the coupon details that's associated with a particular product and category.
On the modify page, I have the following fields
Coupon description - TextField type
Coupon Price - TextField type
Coupon Percentage - TextField type
Category Name - Dropdownfield type
Product Name - Dropdownfield type
I have the following sql query.
$sql = "SELECT cp_description, cp_discountprice, cp_discountpercent, pd_name, cat_name
FROM tbl_coupon inner join (tbl_product, tbl_category) on
(tbl_product.pd_id = tbl_coupon.pd_id AND tbl_category.cat_id=tbl_product.cat_id)
WHERE cp_id = $cpId";
What's really interesting about this problem is that I could not include pd_id and cat_id fields into the sql query so I can manipulate them in another sql code that will look up these fields to retrieve correct selected element in a dropdown field when prepopulated.
I cannot use pd_name or cat_name in select statement because these fields are not 'unique' so can cause problems.
Does anybody have any idea what's the best way to approach this problem? I thought the distinct keyword may do the trick... But it doesn't!
Can't you use a query like this?
SELECT
cp.cp_description, cp.cp_discountprice, cp.cp_discountpercent,
p.pd_id, p.pd_name,
cat.cat_id, cat.cat_name
FROM tbl_coupon cp
INNER JOIN tbl_product p ON p.pd_id = cp.pd_id
INNER JOIN tbl_category cat ON p.cat_id = cat.cat_id
WHERE cp_id = $cpId

SQL query to retrieve products matching one or more filters

I'm writing this message because I ran out of ideas regarding the implementation of the following thing: given a table with products and another one with product filters, I'd like to retrieve the products according to the selected filters.
Tables look like this:
products:
p_id,
p_name,
p_image
product_filters:
pf_id,
pf_product_id,
pf_filter_id,
pf_filter_value
There is a third table with filter name and id but it's not so important.
From the HTML form I get an array with filter id and filter value.
I tried to retrieve the id of the products that matched a selected filter, but it only works file for one selected filter. If there are more than one selected filter I won't get any result.
I'm using PHP and MySQL.
Thanks.
I'm thinking about creating a query like this:
SELECT * FROM products WHERE p_id
IN
(
-- this will be repeated in PHP for every filter id and filter value
SELECT pf_prod_id FROM product_filters
WHERE pf_filter_id = #filterid AND pf_filter_value = #filtervalue
UNION ALL -- omit this line if it's the last filter id
-- end repeat
)
that should do the trick ( although it isn't the prettiest way to solve it probably )
The way I usually do this is by adding a join to the filter table for each filter:
SELECT p_id, p_name, p_image FROM products p
INNER JOIN product_filters pf1 ON p.p_id = pf1.pf_product_id
INNER JOIN product_filters pf2 ON p.p_id = pf2.pf_product_id
// Add another join for each additional filter
WHERE pf1.pf_filter_id = #filterid1 AND pf1.pf_filter_value = #filtervalue1
AND pf2.pf_filter_id = #filterid2 AND pf2.pf_filter_value = #filtervalue2
// Add conditions for each additional filter
You just need to watch out for performance issues when doing this. Do some explains on a couple test queries to make sure your indexes are set up correctly, and it's probably a good idea to put a limit on the number of filters that can be used at one time.

Categories