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;
Related
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
I have a table that holds product names, description, prices, etc and another tables were the admin can add custom columns to expand the first table. These columns are create through php forms. I have no way of knowing how many the user will create.
For example my "products" tables has:
id
name
description
price
created
and the second table "products_custom" has:
id
product_id //this value holds the product id from the above table
serial_number //a custom column the user created
product_image //another column the user created
Now, in order to show all these values in a table i have to LEFT JOIN the 2 tables based on the products.id and products_custom.product_id.
The proper SQL query should be:
SELECT p.*, pc.serial_number, pc.product_image FROM products AS p
LEFT JOIN products_custom as pc ON p.id = pc.product_id ORDER BY p.id ASC
I can get the cell names that exist in the products_custom tables using a php function that queries the 2nd table so having those values in order to use them in the query above is not a problem. I can have these column names returned to a variable.
(example: $mycolumns = "serial_number,product_image")
The problem is that by accident this query here works as well.
SELECT p.*, serial_number,product_image FROM products AS p
LEFT JOIN products_custom ON p.id = products_custom.product_id ORDER BY p.id ASC
Should this be working? Can i leave it like that? or should i add the pc. prefix in every value that $mycolumns has and then reconstruct the sql to make it look like the 'proper query' i wrote above?
Thanks in advance !
FINAL EDIT:
Based on the answers bellow, to convert the SQL to include pc.prefix from a string of column names:
//$whatCol = "serial_number,product_image";
$choices = explode(",", $whatCol);
foreach($choices as &$choice) {
$choice = "pc.".mysql_real_escape_string($choice);
}
$whatColSQL = implode(",", $choices);
$prSQL = "SELECT p.*, $whatColSQL FROM products AS p LEFT JOIN products_custom as pc ON p.id = pc.product_id ORDER BY p.id ASC";
Yes, you should use the pc. table alias.
The reason it works is because those column names are unique to the queried tables.
The reason I say you should use the pc. alias is to prevent a possible issue where a user creates a custom column name (in products_custom) that is also in the products table. For example, description or price. Without the pc. alias a duplicate column name would (should) throw a SQL Error 1052 Ambiguous.
Edit: Included and changed some details based on Drew's comment.
Table-1 parts is a full parts catalog. Primary index field is PartID (int, auto-increment).
Table-2 inventory is a list of partIDs connected to various distributors. Important fields are DistID and PartID. Primary index field is InvID (int, auto-increment).
In Table-2, there will be a unique entry (InvID) for each part a distributor has, so many duplicate DistID/PartID entries.
Given a particular DistID, I must first get a list of all PartIDs associated with that DistID (inventory Table-2), and then SELECT * FROM parts (Table-1) for all those PartIDs.
The end result set is a list of all part information (T1) for each unique part held by a distributor (T2).
I'd like to do this using a single mysql query.
What you want is to start with a distinct list of the inventory for that distributor. This is our base query (replace the question mark with your actual distributor ID):
SELECT DISTINCT partID FROM inventory WHERE DistID = ?
Then we modify that query to join on the parts table to pull back the part information:
SELECT DISTINCT i.partID, p.* FROM inventory i INNER JOIN parts p ON i.partID = p.partID WHERE i.DistID = ?
thats not a PHP question at all.
Anyway, if I understood your question your should try this:
SELECT parts.* FROM parts WHERE parts.PartID IN (SELECT invetory.PartID FROM invetory WHERE invetory.DistID = 'wanted_DistID');
You should change 'wanted_DistID' for you wanted DistID.
Hope I could help.
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
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.