On Sun, 2007-07-01 at 05:46 +0000, Davide.D wrote: > Like this? ( database: mysql )--> > ... > q = request.REQUEST['q'] > ... > from django.db import connection > cursor = connection.cursor() > cursor.execute("SELECT * FROM table_1, table_2, table_3 WHERE > part_number = %s", [q]) > resultset = cursor.fetchone() > > But it doesn't work: > > Exception Type: OperationalError > Exception Value: (1052, "Column 'part_number' in where clause is > ambiguous")
Yes. You have to create legal SQL. So the where constraint will need to be something like "table_1.part_number = %s OR table_2.part_number = % s', etc. Or you could use a UNION statement and an articifical column to tell you which part of the union is appearing in the result. For example, "(SELECT 1, part_number from table1 where ...) UNION (SELECT 2, part_number from table2 where ...)" > > and also the resultset is not a queryset, it's a tuple list? > > [ ( ... ), ( ... ), ( ... ) ] That's correct. You are talking directly to the database and getting back a set of results. You will then know that the first n1 columns in the result are for model1, the next n2 are for model2, etc (what the values of n1 and n2 are will depend on the number of fields in each model). So you can write a function to to find which set of columns aren't empty and create the appropriate model. You are dealing directly with the database here, so normal database interaction rules apply. If you don't want to do that, just construct one queryset for each model and then join the results together in Python. You get to decide which trade-off you prefer: more queries and simpler code (appropriate if you have only a few models and/or not many results) or fewer queries and more complex Python code. Regards, Malcolm -- What if there were no hypothetical questions? http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---