SELECT a.*, c.category_name FROM products a JOIN product_categories b ON a.id = b.product_id
JOIN categories c ON b.category_id = c.id WHERE a.store_id = 20
You can try this:
$data = Yii::app()->db->createCommand('Your query here')->queryAll();
var_dump($data);
Related
Hello i have a query that selects products from a database, but for some reason it returns duplicates. The problem is in my joins i guess since in the product table there is no duplicate product. Here is my query:
$stmt=$dbh->prepare("SELECT
p.name,
p.slug,
p.id_product,
p.price,
pig.image,
c1.slug as ssubcat,
c2.slug as subcat,
c3.slug as cat
FROM
tbl_products p
INNER JOIN tbl_products_to_categories ptoc
ON ptoc.id_product = p.id_product
INNER JOIN tbl_catalog_categories c1
ON ptoc.id_category = c1.id_category
LEFT JOIN tbl_catalog_categories c2
ON c1.id_parent = c2.id_category
LEFT JOIN tbl_catalog_categories c3
ON c2.id_parent = c3.id_category
INNER JOIN tbl_products_images_gallery pig
ON pig.id_product = p.id_product
WHERE (c1.slug = :slug OR c2.slug = :slug OR c3.slug = :slug )
AND p.active = 1
AND p.quantity = 1
ORDER BY p.name ASC
LIMIT $start, $row_limit");
I have this query
SELECT a.*, b.*, c.*, e.*, d.*
FROM categories a
JOIN categories b ON(a.categoryID=b.parentCategory)
JOIN products c ON(b.categoryID=c.categoryID)
LEFT JOIN productprice d ON (d.productID = c.productID)
JOIN currency e ON (e.currencyID = 1)
WHERE a.categoryID in (263,264,265,266,267,268,306,256,0)
ORDER BY a.priority
When I run the query directly in the phpmyadmin I've got correct result and everything is there.
The problem is when I run it on the page and then trying to display productID I've got NULL. This is what I'm trying on the page
$query = "SELECT a.*, b.*, c.*, e.*, d.*
FROM categories a
JOIN categories b ON(a.categoryID=b.parentCategory)
JOIN products c ON(b.categoryID=c.categoryID)
LEFT JOIN productprice d ON (d.productID = c.productID)
JOIN currency e ON (e.currencyID = 1)
WHERE a.categoryID in (263,264,265,266,267,268,306,256,0)
ORDER BY a.priority";
$result= mysql_query($query);
while($row=mysql_fetch_object($result) {
var_dump($row->productID);
}
All $row->productID are NULL.
Update: var_dump($result); return
resource(286) of type (mysql result)
Please try with this query
$query = "SELECT a.*, b.*, c.*, e.*, d.* ,c.productID as productID
FROM categories a
JOIN categories b ON(a.categoryID=b.parentCategory)
JOIN products c ON(b.categoryID=c.categoryID)
LEFT JOIN productprice d ON (d.productID = c.productID)
JOIN currency e ON (e.currencyID = 1)
WHERE a.categoryID in (263,264,265,266,267,268,306,256,0)
ORDER BY a.priority"
I have a inner join query as below,
SELECT * FROM shop_offer so INNER JOIN shop s ON s.shop_id = so.shop_id INNER JOIN city c ON c.city_id = s.city_id INNER JOIN locality l ON l.locality_id = s.locality_id INNER JOIN category ca ON ca.category_id = s.category_id WHERE so.offer_discount >= 10 AND so.publish = 1
While executing the above query in phpmyadmin, the result showing only one row as expected.
But when i tried this query in codeigniter, the result showing 3 rows. My codeigniter code is,
$this->db->select('*');
$this->db->from('shop_offer as so');
$this->db->join('shop as s', 's.shop_id = so.shop_id');
$this->db->join('city as c', 'c.city_id = s.city_id');
$this->db->join('locality as l', 'l.locality_id = s.locality_id');
$this->db->join('category as ca', 'ca.category_id = s.category_id');
$this->db->where(array('so.offer_discount >=' => 10, 'so.publish' => 1));
$query = $this->db->get();
What is the problem in codeigniter code. I am new in codeigniter. Is thre any way to solve this error.
You can also try running the query like this,
$this->db->query("SELECT * FROM shop_offer so
INNER JOIN shop s ON s.shop_id = so.shop_id
INNER JOIN city c ON c.city_id = s.city_id
INNER JOIN locality l ON l.locality_id = s.locality_id
INNER JOIN category ca ON ca.category_id = s.category_id
WHERE so.offer_discount >= 10 AND so.publish = 1");
I have the query below that works fine when I use it in phpMyAdmin, I am just a bit unsure how to do it within the CI framework when I have the m.id etc in place.
Query:
SELECT DISTINCT m.name, m.id, c.id, c.name
FROM `default_ps_products` p
INNER JOIN `default_ps_products_manufacturers` m ON p.manufacturer_id = m.id
INNER JOIN `default_ps_product_x_cats` x ON p.id = x.product_id
INNER JOIN `default_ps_products_categories` c ON x.category_id = c.id
There are many ways.
Example 1:
$result = $this->db
->select('m.name, m.id, c.id, c.name')
->distinct()
->join('default_ps_products_manufacturers m', 'p.manufacturer_id=m.id')
->join('default_ps_product_x_cats x', 'p.id=x.product_id')
->join('default_ps_products_categories c', 'x.category_id=c.id')
->get('default_ps_products p')
->result();
echo $this->db->last_query();
Sometimes the active record can't produce the query you want. So you can write it yourself.
Example 2:
$query = "SELECT DISTINCT m.name, m.id, c.id, c.name
FROM `default_ps_products` p
INNER JOIN `default_ps_products_manufacturers` m ON p.manufacturer_id = m.id
INNER JOIN `default_ps_product_x_cats` x ON p.id = x.product_id
INNER JOIN `default_ps_products_categories` c ON x.category_id = c.id";
$result = $this->db
->query($query)
->result();
echo $this->db->last_query();
In this second example, db::query() can take an array as the second parameter that will replace any question marks (?) within $query with the respective value. For example say you needed to add some where values to your query.
Example 3:
$query = "SELECT DISTINCT m.name, m.id, c.id, c.name
FROM `default_ps_products` p
INNER JOIN `default_ps_products_manufacturers` m ON p.manufacturer_id = m.id
INNER JOIN `default_ps_product_x_cats` x ON p.id = x.product_id
INNER JOIN `default_ps_products_categories` c ON x.category_id = c.id
WHERE c.id=?";
$result = $this->db
->query($query, array(1))
->result();
echo $this->db->last_query();
I'm a little confused in here and need some help...
the situation is I've made three tables(fr_Leagues, fr_nations and fr_confeds), all i want to do is add a league which shows the name of the categories not the i.d with pagination. Here is the code:
NOW FIXED!
"SELECT
a.id as confed_id,
a.fr_short_name as confed_name,
b.id as nation_id,
b.fr_name as nation_name,
c.id as league_id,
c.fr_name as league_name"
." FROM fr_confeds as a
INNER JOIN fr_nations as b ON a.id = b.confed_id
INNER JOIN fr_leagues as c ON b.id = c.nation_id"
." LIMIT $paginate->start, $paginate->limit"
You are missing on how to link the different tables together. On each INNER JOIN, you need to have it:
INNER JOIN fr_nations ON a.<someColumn> = b.<anotherColumn> INNER JOIN fr_leagues ON a.<someColumn> = b.<anotherColumn>
USE THIS QUERY
SELECT * FROM fr_confeds as A
INNER JOIN fr_nations as B ON A.id = B.confed_id
INNER JOIN fr_leagues as C ON B.confed_id = C.league_id
LIMIT $paginate->start, $paginate->limit