I have product details in tne table, say products, categories and brand names in different tables say categories and brand. And I want to join these in a query. The query I use is,
$sql = "
SELECT p.pid
, p.name
, p.slug
, p.category
, c.name
, p.brand
, b.name
FROM products p
JOIN categories c
ON c.sno = p.category
JOIN brand
ON b.sno = p.brand
WHERE p.sku=?
";
This query does not return any rows. However there are data in products, categories and brand tables.
Is there any logic failure in my query?
Products table
Categories table
Brand table
This is a bit long for a comment.
For the data that you have shown, your query should work. I would start by removing the where clause and running:
SELECT products.pid, products.name, products.slug, products.category, categories.name, products.brand, brand.name
FROM `products`
INNER JOIN `categories` on products.category = categories.sno
INNER JOIN `brand` on products.brand = brand.sno ;
(You should run this directly on the database; if there is a lot of data, just throw in a limit 100 to see if you get anything.)
If this doesn't return anything, then you have a problem with the joins. Look at the types of the fields. Are they the same? If not, fix the data structure. If they are characters, check for leading spaces and hidden characters.
If that query returns what you expect, the the problem is matching the skus. The most likely problem is the assignment of the value in php. Check that this is really doing what you want. Once again, check for leading spaces in the sku column and in the value. If this is the problem, you can use trim() to fix the problem.
Related
Good Day.
I know this question is already asked but I have a hard time implementing my problem.
I want to Join 3 tables. Here is how my tables look like:
Order_Header(Order_Header_ID{PK}, Order_Date, Order_Time, Order_Complete)
Order_Line(Order_Line_ID{PK}, Product_ID{FK}, Order_Header_ID{FK},Quantity)
Products(Product_ID{PK}, Description, Cost)
I Want to JOIN the three tables so that in one table it displays Order_Header_ID, Quantity and Description out of the three tables WHERE Order_Complete = 'YES'.
I have the following SQL that displays all the information but do not join them.
SELECT
Order_Header.Order_Header_ID,
Products.Description,
Order_Line.Quantity
FROM Order_Header, Products, Order_Line
WHERE Order_Complete = 'yes'
The idea is that it list a Order_Header_ID once with the corresponding Description and Quantity only once.
Thank you in advance
Your current query is missing join conditions. Also, it is preferable to use an explicit join syntax. Correcting for both of these things we can write the following query:
SELECT
oh.Order_Header_ID,
p.Description,
ol.Quantity
FROM Order_Header oh
INNER JOIN Order_Line ol
ON oh.Order_Header_ID = ol.Order_Header_ID
INNER JOIN Products p
ON ol.Product_ID = p.Product_ID
WHERE
oh.Order_Complete = 'yes'
I have two tables for an example: Posts and Categories. Posts are assigned a category, but by an ID number. When grabbing my posts, I would like to perform a LEFT JOIN and get the name of the category associated with that post, then switch the output - not the actual table content - to include the name directly in place of the value. So far, this is what I have, and I know it works to a point up to the LEFT JOIN, but the replacing values is where I get confused, though I believe it is possible
SELECT * FROM nve_multi_posts
LEFT JOIN nve_categories
SELECT
CASE
WHEN nve_multi_posts.category = nve_categories.id
THEN
nve_multi_posts.category = nve_categories.name
END
ON nve_multi_posts.category = nve_categories.id
What am I messing up?
Just that simple:
SELECT p.*, c.*
FROM nve_multi_posts p
LEFT JOIN nve_categories c
ON p.category = c.id
Or do you want some kind of UPDATE query to chenge data stored in nve_multi_posts table?
If you just need named column returned from the query you can just alias every column you need like:
SELECT p.id as post _id,
p.category as category_id,
c.name as category
FROM nve_multi_posts p
LEFT JOIN nve_categories c
ON p.category = c.id
I am try to get a count of all missing translations and getting really lost in how to do it.
Tables:
languages (language_id, name)
products (product_id)
product_translations (product_id, language_id, name)
The admin are getting very lazy and I want to be able to show them a count of how many translations are missing.
I guess a very simple was of doing this would be to just get the total (languages->count * products->count) but I wanted to return a count for each product separately.
To do such a query, start with a driver table (subquery) that has all combinations. Then remove the ones that have translations:
select driver.*
from (select distinct l.language_id, p.product_id
from languages l cross join
products p
) driver left outer join
translations t
on t.language_id = driver.language_id and
t.product_id = driver.product_id
where t.language_id is null;
This uses a left outer join, which keeps everything in the driver table. If there is no match, then the columns in translations will be NULL -- the where clause keeps only these.
The distinct may not be necessary in the subquery, if the values in each table are unique.
As a note: the above is my preferred way to write the query, because I think it is the clearest in intent. But, MySQL actually materializes the subquery. So the following is more efficient, if the columns are unique in the two reference tables:
select l.*, p.*
from languages l cross join
products p left outer join
translations t
on t.language_id = l.language_id and
t.product_id = p.product_id
where t.language_id is null;
I'm trying to query my database for all my products and when I do that it's crashing the server and I can't get the information
SELECT categories.category_id,
categories.category_name,
categories.category_name_fr,
subcategories.subcategory_id,
subcategories.subcategory_name,
subcategories.subcategory_name_fr,
suppliers.supplier_id,
suppliers.supplier_name,
products.product_id,
products.product_name,
products.product_name_fr,
products.product_url
FROM categories,
subcategories,
suppliers,
products
WHERE products.product_active = 1
ORDER BY categories.category_id ASC
I tried putting a LIMIT 0, 1 and it STILL crashed...
First, you should explicitly specify your joins and join conditions, as right now you are getting a Cartesian product of all the tables, which is likely not what you want.
You should list the products table first amongst the joined tables, as this is the table where your WHERE clause filtering is in effect. So something like:
SELECT categories.category_id, categories.category_name,
categories.category_name_fr, subcategories.subcategory_id,
subcategories.subcategory_name, subcategories.subcategory_name_fr,
suppliers.supplier_id, suppliers.supplier_name, products.product_id,
products.product_name, products.product_name_fr, products.product_url
FROM products
INNER JOIN categories ON products.[some field] = categories.[some field]
INNER JOIN subcategories ON categories.[some field] = subcategories.[some field]
INNER JOIN suppliers ON products.[some field] = suppliers.[some field]
WHERE products.product_active = 1
ORDER BY categories.category_id ASC
Second, you need to make sure you have an index on each of products.product_active and categories.category_id as well as any other columns used in the join conditions.
Third, just as an FYI, a LIMIT condition would do nothing to help the query execution time here as you still need to perform the join, the filter, and the sort before deciding to limit the result set to just a subset of the items. The only place where it would help would be in processing through the result set (which would obviously happen much quicker).
What I want to do is a PHP script that gets all the products of a Joomla VirtueMart using PHP and mySQL. However my question has nothing to do about Joomla or PHP but I am in the need of a mySQL query.
Below you can find the tables and the fields that holds the data I need which are
product_id, product_desc, product_full_image, product_name, product_price, category_name.
To get the product_price you will need the product_id from jos_vm_product and match it with the product_id from jos_vm_product_price to get the product_price.
Every product_id has a category_id inside jos_vm_product_category_xref. So, when the category_id is found, you get the category_name from jos_vm_category.
This is difficult for me as I am not very familiar with SQL queries and I need your help on how to to join those tables, so it will allow me to echo the values I need.
Thank you.
jos_vm_poduct
product_id
product_desc
product_full_image
product_name
jos_vm_product_price
product_id
product_price
jos_vm_product_category_xref
product_id
category_id
jos_vm_category
category_id
categroy_name
Sure we could write you an answer, but, to best understand it heres the things you want to know
select <stuff> from <table1>
http://dev.mysql.com/doc/refman/5.0/en/join.html
You then join it with another table
so,
select product_name, product_price from table1
left join table2 on table1.product_id=table2.product_id
once you've linked your tables.
Now strip out the bits you either want, or dont want. Such as, if you left join.. you may want
where product_price>0
When you have then stripped or selected the bit(s) you wanted, job done.
Something like this ought to work:
SELECT jvp.product_id, product_desc, product_full_image, product_name, product_price, category_name
FROM jos_vm_product jvp
JOIN jos_vm_product_price jvpp
ON jvp.product_id = jvpp.product_id
JOIN jos_vm_product_category_xref jvpcx
ON jvpcx.product_id = jvp.product_id
JOIN jos_vm_category jvc
ON jvc.category_id = jvpcx.category_id
Any questions about why, please ask away!
I'm assuming you want all the products of a given category. If not it's easy to remove the where clause to grab them all.
I used a subquery to fetch all the product id's of a given category, then join the other two tables.
SELECT
*
FROM
(SELECT
p.product_id,
c.category_name,
FROM
jos_vm_product_category_xref AS p
LEFT JOIN jos_vm_category AS c
ON p.category_id == c.category_id
WHERE
c.category_id = 4
) as products
LEFT JOIN jos_product
ON jos_product.product_id=products.product_id
LEFT JOIN jos_vm_product_price
ON jos_vm_product_price=products.product_id