0

I've been working on a project that until now has only needed to find 1 row from the joined table. But now I need to grab multiple rows..

So as it stand my sql works something like:

Select rows for each company for this particular project which alone would find company details (name, id, telephone.. blah). Then I join a table that contains form data submitted for each company (multiple forms - so multiple records)

Until now i have been specifying one formid to look for in the join, but now i need to specify multiple ones.

If I use WHERE form_id = 1 OR form_id = 2 OR form_id = 3 ... I get a result of only the first form match that is found per company..

If I mix up the query so it looks for the forms 1st and returns multiple records for each company with different form data - that works in this sense..

But I am then looping through this array in a view and creating a table row per record (previously each row was a new company) but using the latter would cause multiple records to show for the same company.

Any way I can do this? I tried group by with the latter method but this results in only 1 record again.

    SELECT DISTINCT p.project_company_has_user_id, p.project_company_has_user_project_id, p.project_company_has_user_user_id, c.company_id, c.company_hall_no, c.company_company_name, c.company_type, c.company_country, c.company_stand_number, c.company_image_file_1, p2.project_id, p2.project_name, u.user_id, u.user_username, o.orders_id, o2.order_detail_id, o2.order_detail_product_id, f2.form_question_has_answer_id, f2.form_question_has_answer_request, f2.form_question_has_answer_form_id, f2.form_question_has_answer_user_id 
    FROM project_company_has_user p 
    INNER JOIN company c ON p.project_company_has_user_company_id = c.company_id 
    INNER JOIN project p2 ON p.project_company_has_user_project_id = p2.project_id 
    INNER JOIN user u ON p.project_company_has_user_user_id = u.user_id 
    INNER JOIN form f ON p.project_company_has_user_project_id = f.form_project_id 
    LEFT JOIN orders o ON p.project_company_has_user_user_id = o.orders_user_id 
    LEFT JOIN order_detail o2 ON ((o2.order_detail_orders_id = o.orders_id AND (o2.order_detail_product_id = 65 OR o2.order_detail_product_id = 68 OR o2.order_detail_product_id = 64))) 
    LEFT JOIN form_question_has_answer f2 ON ((f2.form_question_has_answer_form_id = 297 AND f2.form_question_has_answer_user_id = p.project_company_has_user_user_id)) 
    WHERE (f.form_template_name = "custom" AND p.project_company_has_user_garbage_collection = 0 AND p.project_company_has_user_project_id = 48) AND (LCASE(c.company_country) LIKE "%uk%" OR LCASE(c.company_country) LIKE "%uk%") ORDER BY company_company_name asc
1
  • Could you post your current SQL query?
    – weenoid
    Apr 20, 2012 at 9:09

2 Answers 2

1

you need another field in order_detail as o2 . this field is row_index(position),etc for positioning record

LEFT JOIN order_detail o2 ON (o2.row_index=1 AND (o2.order_detail_orders_id = o.orders_id AND (o2.order_detail_product_id = 65 OR o2.order_detail_product_id = 68 OR o2.order_detail_product_id = 64))) 
0

Personally I would use an Outer Join for the table of which elements you need to list all matches. Should you them need to clean up that data you can build the logic into the Join Condition (as step 2). Depending on the volume of data you are handling and whether or not you need to reuse it later in the same proc, you may want to post that primary dataset into a temp table and use that as source (primary) for your later logic.

Hope that helps. If you need the code, let me know, but it is pretty straight forward.

Regards

Mac

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.