I try to write a query in order to retrieve full info for our product in virtuemart 2.
First I try with single INNER JOIN like the example bellow:
$query ="select p.`virtuemart_product_id`, p.`product_name`, p.`product_s_desc`, `pmqbw_virtuemart_product_categories`.`virtuemart_category_id`
FROM `pmqbw_virtuemart_products_el_gr` AS p
INNER JOIN `pmqbw_virtuemart_product_categories` ON p.`virtuemart_product_id`=`pmqbw_virtuemart_product_categories`.`virtuemart_product_id`
LIMIT 0 ,10";
The above worked well!
Then I tried to be more complex and I wrote the following:
$query ="select p.`virtuemart_product_id`, p.`product_name`, p.`product_s_desc`, `pmqbw_virtuemart_product_categories`.`virtuemart_category_id`, `pmqbw_virtuemart_categories_el_gr`.`category_name`
FROM `pmqbw_virtuemart_products_el_gr` AS p
INNER JOIN p
ON `pmqbw_virtuemart_product_categories`.`virtuemart_product_id`=p.`virtuemart_product_id`
INNER JOIN `pmqbw_virtuemart_categories_el_gr` AS x
ON x.`virtuemart_category_id`=`pmqbw_virtuemart_product_categories`.`virtuemart_category_id`
LIMIT 0 ,10";
But something goes wrong here and I cannot understand.
After that I'll try to retrieve more tables and more joins in order to bring product data I need.
Thank you for your help in advance!
Actually I want to retrieve data from the following tables.
From pmqbw_virtuemart_products_el_gr I want fields: product_name, product_s_desc,virtuemart_product_id`
From pmqbw_virtuemart_product_categories I want fields: virtuemart_product_id, virtuemart_category_id
From pmqbw_virtuemart_categories_el_gr I want fields: virtuemart_category_id, category_name
From pmqbw_virtuemart_product_medias I want fields: virtuemart_product_id, virtuemart_media_id
pmqbw_virtuemart_medias I want fields: virtuemart_media_id, file_url, file_url_thumb where file_mimetype=image/jpeg AND file_type=product
I tried a different query with left joins but does not work as well.
$sql = "SELECT N.product_name,I.virtuemart_media_id,M.virtuemart_product_id,MN.category_name
FROM pmqbw_virtuemart_categories_el_gr AS M
LEFT JOIN pmqbw_virtuemart_products AS P ON P.virtuemart_product_id = M.virtuemart_product_id
LEFT JOIN pmqbw_virtuemart_products_el_gr AS N ON N.virtuemart_product_id = M.virtuemart_product_id
LEFT JOIN pmqbw_virtuemart_product_medias AS I ON I.virtuemart_product_id = M.virtuemart_product_id
LEFT JOIN pmqbw_virtuemart_product_categories AS MN ON MN.virtuemart_category_id = M.virtuemart_category_id
WHERE M.virtuemart_category_id = 1";
Any help will be appreciated!
Thank you!
Thank you all for the help!
the query that runs ok is the following:
$sql = "SELECT DISTINCT X.virtuemart_product_id, X.product_name, M.category_name, Y.virtuemart_product_id, W.file_url
FROM pmqbw_virtuemart_products_el_gr AS X
INNER JOIN pmqbw_virtuemart_product_categories AS P ON P.virtuemart_product_id = X.virtuemart_product_id
AND P.virtuemart_category_id =2
INNER JOIN pmqbw_virtuemart_categories_el_gr AS M ON M.virtuemart_category_id = P.virtuemart_category_id"
INNER JOIN pmqbw_virtuemart_product_medias AS Y ON Y.virtuemart_product_id = X.virtuemart_product_id
INNER JOIN pmqbw_virtuemart_medias AS W ON W.virtuemart_media_id = Y.virtuemart_media_id;
The bud thing is that every product has two or more images and I only want one.
I have to think something in order to eliminate it!
If anyone has something in his mind for help will be more than welcome!
Thank you!
Ok, I find a solution with GROUP BY Y.virtuemart_product_id
:)
Thank you all !
Related
I'm trying to join four tables together. The code works fine when I'm only comparing the EVENT_DATA and joining the PERSONA tables as I'm able to get the "Name" column from PERSONA (which doesn't exist in the EVENT_DATA table). However, one of the problems is that this "Name" column also exists in the CUSTOMCAR table, so I can only get one or the other. Additionally, when I tried adding the last join statement, the code wouldn't run at all.
If someone could please help me, that would be great!
$sql = "SELECT * FROM EVENT_DATA
LEFT JOIN PERSONA ON EVENT_DATA.personaId = PERSONA.ID
LEFT JOIN CUSTOMCAR ON CUSTOMCAR.ownedCarId = EVENT_DATA.carId
LEFT JOIN CARCLASSES ON CARCLASSES.store_name = CUSTOMCAR.name
WHERE (EVENT_DATA.EVENTID = '299')";
You should avoid * and use explicit columns list instead:
SELECT EVENT_DATA.personaId, ...
FROM EVENT_DATA
LEFT JOIN PERSONA ON EVENT_DATA.personaId = PERSONA.ID
LEFT JOIN CUSTOMCAR ON CUSTOMCAR.ownedCarId = EVENT_DATA.carId
LEFT JOIN CARCLASSES ON CARCLASSES.store_name = CUSTOMCAR.name
WHERE (EVENT_DATA.EVENTID = '299');
If you have same column name you need to to use aliasing:
SELECT ...
CUSTOMCAR.NAME AS c_name,
PERSONA.NAME AS p_name
...
You could also use Aliasing:
$sql = "SELECT ed.*,
pa.*,
cc.*,
ccs.*
FROM EVENT_DATA AS ed
LEFT JOIN PERSONA AS pa ON ed.personaId = pa.ID
LEFT JOIN CUSTOMCAR AS cc ON cc.ownedCarId = e.carId
LEFT JOIN CARCLASSES AS ccs ON ccs.store_name = cc.name
WHERE (ed.EVENTID = '299')";
Note: Although as suggested by #Lukasz, you should really avoid using wildcard (*), and provide an explicit list of columns in the SELECT clause.
Hello all StackOverFlow families.
I need your help about sql query in mysql. I join four tables but result is duplicate row.
I have tried by using GROUP BY but not work.
Here is my query:
SELECT `tbl_leave`.`id`, `tbl_leave`.`staff_id`, `tbl_leave`.`type_id`, `tbl_leave`.`start_date`, `tbl_leave`.`end_date`,
`tbl_leave`.`total_days`, `tbl_leave`.`reason`, `tbl_leave_type`.`type`, `tbl_employment`.`com_id` as `comid`, `tbl_staff`.`name`
FROM `tbl_leave` JOIN
`tbl_leave_type`
ON `tbl_leave_type`.`id` = `tbl_leave`.`type_id` JOIN
`tbl_employment`
ON `tbl_employment`.`staff_id` = `tbl_leave`.`staff_id` JOIN
`tbl_staff`
ON `tbl_staff`.`id` = `tbl_leave`.`staff_id`
You can look as picture: https://imgur.com/gallery/1Ewku
And this is relationship table: https://imgur.com/gallery/ziKq3
The result I want like this : https://imgur.com/gallery/6NJpR
Thank for your valuable times for this question.
Please try
SELECT distinct `tbl_leave`.`id`, `tbl_leave`.`staff_id`,
`tbl_leave`.`type_id`, `tbl_leave`.`start_date`, `tbl_leave`.`end_date`,
`tbl_leave`.`total_days`, `tbl_leave`.`reason`, `tbl_leave_type`.`type`,
`tbl_employment`.`com_id` as `comid`, `tbl_staff`.`name`
FROM `tbl_leave` JOIN
`tbl_leave_type`
ON `tbl_leave_type`.`id` = `tbl_leave`.`type_id` JOIN
`tbl_employment`
ON `tbl_employment`.`staff_id` = `tbl_leave`.`staff_id` JOIN
`tbl_staff`
ON `tbl_staff`.`id` = `tbl_leave`.`staff_id`;
Please try this solution and let me know if is there any problem occur:
SELECT [Columns] From tbl_staff as staff
JOIN tbl_employment as emp on staff.id = = emp.staff_id
JOIN tbl_leave as leave on staff.id = = leave.staff_id
JOIN tbl_leave_type as ltype on leave.type_id = = ltype.id
I have the following Mysql query. Running as needed.
SELECT
t_doc.*, t_user.IDA, t_data.IDA
FROM
( SELECT DISTINCT IDau FROM t_doc ) IDau_list
LEFT OUTER JOIN
(
SELECT
IDd ,IDau
FROM
t_doc
WHERE
doc_type = 'doc'
GROUP BY IDau
)
doc_images ON doc_images.IDau = IDau_list.IDau
LEFT OUTER JOIN
(
SELECT
IDd ,IDau
FROM
t_auto_doc
WHERE
doc_type = 'jpg'
GROUP BY IDau
)
jpg_images ON jpg_images.IDau = IDau_list.IDau
LEFT OUTER JOIN
t_doc ON t_doc.IDdoc = COALESCE(jpg_images.IDd, doc_images.IDd)
LEFT OUTER JOIN
t_user ON t_user.IDau = t_doc.IDau
LEFT OUTER JOIN
t_data ON t_user.IDA = t_data.IDA
But, What I must bring this query to codeigniter Model, in which I have to adapt the query, as example like this...
$this->db->select('u.IDau, a.*');
$this->db->from('t_user u');
$this->db->join('t_doc d', 'd.IDau=u.IDau', 'left');
And ...More.....Here......
But, I got it difficult. Is some one out there might change it Please..
Thanks in advance
As #NEVERMIND Suggest I have accomplish the task using...
$query = $this->db->query("My QUERY WITH A BIT OF CHANGE");
Yes, I should make some change to get the exact Data as i needed.
Thanks for all of your comments and ideas.
I would like extract in SQL (no php) the id, image url, thumb url and price of all the products in a specific subcategory in Magento.
I'm using this approach but i don't know where I can find the other information (price, image url, thumb url).
Can you help me, please?
This is my approach:
SELECT *
FROM mg_catalog_product_entity AS p
LEFT JOIN mg_catalog_category_product AS cp
ON p.entity_id = cp.product_id
LEFT JOIN mg_catalog_category_entity AS c
ON cp.category_id = c.entity_id
LEFT JOIN mg_catalog_category_entity_varchar AS cat_varchar
ON c.entity_id = cat_varchar.entity_id AND cat_varchar.attribute_id = 111
LEFT JOIN mg_catalog_product_entity_varchar AS prod_varchar
ON p.entity_id = prod_varchar.entity_id AND prod_varchar.attribute_id = 96
WHERE 1
AND c.entity_id = 4;
Thank you so much!
The simplest approach would be to left join all of
`catalog_product_entity_*` tables that exist in Magento
And use catalog_eav_attribute to get the keys to append the labels onto the left joined values from the eav tables.
After filter the data needed.
If you want to simplify the joining, turn on database debug, check the output when the you load a product page or admin product page and replicate the joins used by the orm:
m1\lib\Varien\Db\Adapter\Pdo\Mysql.php
Line 103:
protected $_debug = true;
Line 117:
protected $_logAllQueries = true;
Check output in:
m1\var\debug\pdo_mysql.log
You can use the following query for everything except the thumb, simply replace the category_id in the WHERE clause to whatever category you want to analyze:
SELECT DISTINCT catalog_product_entity_media_gallery.entity_id AS id,
catalog_product_entity.sku,
catalog_product_entity_media_gallery.value AS image url,
catalog_product_index_price.price AS price
FROM ((magento.catalog_product_entity_media_gallery
catalog_product_entity_media_gallery
INNER JOIN magento.catalog_product_entity catalog_product_entity
ON (catalog_product_entity_media_gallery.entity_id =
catalog_product_entity.entity_id))
INNER JOIN
magento.catalog_product_index_price catalog_product_index_price
ON (catalog_product_index_price.entity_id =
catalog_product_entity.entity_id))
INNER JOIN magento.catalog_category_product catalog_category_product
ON (catalog_category_product.product_id =
catalog_product_entity.entity_id)
WHERE (catalog_category_product.category_id = '23')
ORDER BY catalog_product_entity_media_gallery.entity_id ASC
My query is as follows:
SELECT collection_content_mappings.tbl_content_common_id
FROM collection_content_mappings Inner Join tbl_content_commons ON collection_content_mappings.tbl_content_common_id = tbl_content_commons.id
INNER JOIN tbl_content_additionals ON collection_content_mappings.tbl_content_common_id = tbl_content_additionals.content_content_code
WHERE collection_content_mappings.collection_id = 1
This part is the problem:
collection_content_mappings.tbl_content_common_id = tbl_content_additionals.content_content_code
The field "collection_content_mappings.tbl_content_common_id" is not the one that need to be equated there; instead I have to take tbl_content_commons.content_common_code from tbl_content_commons where collection_content_mappings.tbl_content_common_id = tbl_content_commons.id . How do I add this to the query above?
Any help is appreciated
You should really rename your tables when you are building the query.
I don't know if I understand properly what you want, but I think that is something like this:
SELECT tbl_content_commons.id
FROM tbl_content_commons
Inner Join collection_content_mappings ON collection_content_mappings.tbl_content_common_id = tbl_content_commons.id
INNER JOIN tbl_content_additionals ON tbl_content_additionals.content_content_code = tbl_content_commons.content_common_code
WHERE tbl_content_commons.id = 1