Help nested sql


 
Thread Tools Search this Thread
Top Forums Programming Help nested sql
# 1  
Old 05-29-2011
Help nested sql

Hi, I bought a module called "Price comparison listing" for Prestashop that was supposed to help me push product listing to kelkoo. Unfortunately it doesnt work and the developers don't fix, and they don't refund.... I have reported this to prestashop, but I need a solution..

Be careful buying anything from prestashop.. This was a community addon, but on prestashop webshop.

Problem is this sql statement;

PHP Code:
    $productsSQL 'SELECT p.additional_shipping_cost, p.on_sale, m.name AS manufacturer,p.reference,p.supplier_reference,p.weight,p.price, pl.description_short,p.id_tax_rules_group, p.id_product, p.quantity, pl.link_rewrite, cl.`link_rewrite` category, ean13, pl.name,
    (SELECT id_image FROM '
._DB_PREFIX_.'image WHERE `cover` = 1 AND id_product=p.id_product) AS id_image
    FROM '
._DB_PREFIX_.'product p
    LEFT JOIN '
._DB_PREFIX_.'product_lang pl ON (p.id_product = pl.id_product)
    LEFT JOIN `'
._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND pl.`id_lang` = cl.`id_lang`)
    LEFT JOIN '
._DB_PREFIX_.'manufacturer m ON p.id_manufacturer = m.id_manufacturer
    WHERE p.`active` = 1 AND pl.`id_lang` = '
.$id_lang.'
    ORDER BY p.id_product ASC LIMIT '
.$limitStart.','.(int)Tools::getValue('toSendEachIteration');
    
$products Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($productsSQL);
    if(
$firstrow
It works but will give you only the category of the product, not the entire category path that kelkoo demands.

The database looks like this;
It has a table called ps_category that contains, among other, these fields:
id_category id_parent level_depth

Above sql statement finds the id of the category, but should also look for id_parent (and level_depth?) to include top category level(s). I have maximum 3 levels, like "phone"->"mobile phone"->"iphone" where the statement above gives you only "iphone" and nothing else..

I don't know how to fix the above sql statement so it includes even top level categories, if there.

Any help would be highly appreciated since I'm kind of stuck here with a module that is unusable... but paid for..

---------- Post updated at 12:10 PM ---------- Previous update was at 09:13 AM ----------

I guess this is the part of the sql-statement that needs adjusting;
PHP Code:
LEFT JOIN `'._DB_PREFIX_.'category_langcl ON (p.`id_category_default` = cl.`id_category` AND pl.`id_lang` = cl.`id_lang`) 
In table ps_category it should look for cl.`id_category` as above, but if `id_parent` on the same row is not = 0 then it should take id_parent and look that id up in id_category, because thats the toplevel of categories.
Now if that toplevel category also has `id_parent` not =0 it should again take that id and look it up in `id_category` because then the product has a three step path in categories.

Then it should merge those category steps into one name like:
categorylevel1.categorylevel2.categoryofproduct and use that as a value for cl.`id_category` instead of only categoryofproduct as it does now.

Does this make sense? Can you even do it with mysql?

Any help or input would be appreciated.

---------- Post updated at 01:25 PM ---------- Previous update was at 12:10 PM ----------

This is how far I come by my self:

PHP Code:
SELECT p.additional_shipping_costp.on_salem.name AS manufacturer,p.reference,p.supplier_reference,p.weight,p.pricepl.description_short,p.id_tax_rules_groupp.id_productp.quantitypl.link_rewritecl.`link_rewritecategoryean13pl.name,
(
SELECT id_image FROM ps_image WHERE `cover` = AND id_product=p.id_product) AS id_image
FROM ps_product p
LEFT JOIN ps_product_lang pl ON 
(p.id_product pl.id_product)
LEFT JOIN `ps_category_langcl ON (p.`id_category_default` = cl.`id_category` AND pl.`id_lang` = cl.`id_lang`)

*******************
Looking for toplevel category...

LEFT JOIN ps_category pc (ON cl.`id_category`= pc.`id_category`)

>>
Trying to figure out if id_parent is bigger than 0 then look id_parent up...
IF 
pc.`id_parent` > 0 THEN 

LEFT JOIN 
`ps_category_langcl ON (pc.`id_parent` = cl.`id_category` AND pl.`id_lang` = cl.`id_lang`)

Looking for top-top-level category...
LEFT JOIN ps_category pc (ON cl.`id_category`= pc.`id_category`)

Trying to figure out if id_parent is bigger than 0 then look id_parent up...
IF 
pc.`id_parent` > 0 THEN 
LEFT JOIN 
`ps_category_langcl ON (pc.`id_parent` = cl.`id_category` AND pl.`id_lang` = cl.`id_lang`)

put all category levels together as such level1.level2.category and preferebly assign this to `category`in top SELECT so it fits with the rest of the script.

*******************
LEFT JOIN ps_manufacturer m ON p.id_manufacturer m.id_manufacturer
WHERE p
.`active` = AND pl.`id_lang` = 6
ORDER BY p
.id_product ASC 
I hope it makes sense so someone can see what I try to do. Please help, I would hate to pay more money to make this work since I already payed for it but without being able to use it or get support from those scams selling this...
# 2  
Old 05-31-2011
Well, a product may have 1 to N parent categories, so you need a querying mechanism that can recurse the categories dynamically. Oracle has this, not sure about others.

You might do an N way join and case it so if this product's depth is not N, the missing categories resolve to '' and every n+1 query knows also case to ''. Now, you can concatenate categories and report the path.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

2. Shell Programming and Scripting

Nested if else

Hi, i m trying to create script which logic is like below. if ; then x=`cat /tmp/testoutput.log | grep STOP | wc -l` y=`cat /tmp/testoutput.log | grep RUN | wc -l` if ; then echo "process stop" if ; then echo "process running " else echo "file not found" fi ----------------... (2 Replies)
Discussion started by: tapia
2 Replies

3. UNIX for Advanced & Expert Users

Call parallel sql scripts from shell and return status when both sql are done

Hi Experts: I have a shell script that's kicked off by cron. Inside this shell script, I need to kick off two or more oracle sql scripts to process different groups of tables. And when both sql scripts are done, I will continue in the shell script to do other things like checking processing... (3 Replies)
Discussion started by: huasheng8
3 Replies

4. Shell Programming and Scripting

Execute multiple SQL scripts from single SQL Plus connection

Hi! I would like to do a single connection to sqlplus and execute some querys. Actually I do for every query one connection to database i.e echo 'select STATUS from v$instance; exit' > $SQL_FILE sqlplus user/pass@sid @$SQL_FILE > $SELECT_RESULT echo 'select VERSION from v$instance;... (6 Replies)
Discussion started by: guif
6 Replies

5. Shell Programming and Scripting

Nested SQL queries within Shell script

Hi, Would someone know if I can fire nested sql queries in a shell script? Basically what I am trying to do is as follows: my_sql=$(sqlplus -s /nolog<<EOF|sed -e "s/Connected. *//g" connect... (2 Replies)
Discussion started by: shrutihardas
2 Replies

6. Shell Programming and Scripting

How to use sql data file in unix csv file as input to an sql query from shell

Hi , I used the below script to get the sql data into csv file using unix scripting. I m getting the output into an output file but the output file is not displayed in a separe columns . #!/bin/ksh export FILE_PATH=/maav/home/xyz/abc/ rm $FILE_PATH/sample.csv sqlplus -s... (2 Replies)
Discussion started by: Nareshp
2 Replies

7. UNIX for Dummies Questions & Answers

Execute PL/SQL function from Unix script (.sql file)

Hi guys, I am new on here, I have a function in oracle that returns a specific value: create or replace PACKAGE BODY "CTC_ASDGET_SCHED" AS FUNCTION FN_ASDSCHEDULE_GET RETURN VARCHAR2 AS BEGIN DECLARE ASDSchedule varchar2(6); ASDComplete... (1 Reply)
Discussion started by: reptile
1 Replies

8. Shell Programming and Scripting

unix variables from sql / pl/sql

How do I dynamically assign the below output to unix shell variables so I can build a menu in a shell script? Example: var1 = 1 var2= SYSTEM var3 = 2 var4= UNDOTBS1 and so on, then in the shell script I can use the variables to build a menu. set serveroutput on declare... (2 Replies)
Discussion started by: djehres
2 Replies

9. Shell Programming and Scripting

Calling SQL LDR and SQL plus scripts in a shell script

Hi- I am trying to achieve the following in a script so I can schedule it on a cron job. I am fairly new to the unix environment... I have written a shell script that reads a flat file and loads the data into an Oracle table (Table1) via SQLLDR. This Works fine. Then, I run a nested insert... (5 Replies)
Discussion started by: rajagavini
5 Replies

10. Shell Programming and Scripting

can nested SQl be run in Unix Script?

can nested SQl be run in Unix Script? I tried some and found only simply sql(one select) can work well in unix script. (21 Replies)
Discussion started by: YoYo
21 Replies
Login or Register to Ask a Question