How to use NOT IN sql query in django [duplicate] - sql

This question already has answers here:
Django equivalent of SQL not in
(4 answers)
Closed 3 years ago.
Im new to Django. Anyone help me to write "Not In" sql query in django.
here i used raw() query set without raw() query set how to write this query in django.
query = 'SELECT basic_uom FROM uom_master WHERE id="'+ id +'" and basic_uom not in (SELECT next_uom from uom_master WHERE id="'+ id +'") and basic_uom not in(SELECT std_uom FROM std_uom_master WHERE id"'+ id +'")ORDER BY next_uom ASC'
data = uom_master.objects.raw(query)

If [1,2,3] is your list then you can do something like this, you modify according to your table names
uom_master.objects.filter(id=some_id).exclude(id__in=[1,2,3]).order_by("next_uom")
To get list of ids you can do like
list_of_ids = std_uom_master.objects.filter().values_list("id",flat=True)
filter is your filtering criteria and exclude is records to be omitted.
See filters like id__in and guide about making queries

Related

<<-sql in Ruby to get a relation? [duplicate]

This question already has an answer here:
Rails relation ordering?
(1 answer)
Closed 1 year ago.
So I have a function like this:
def get_relation(my_offset, my_limit):
query = <<-SQL
SELECT *
FROM my_table
Limit my_limit <--- HERE
Offset my_offset <--- HERE
Order by id
SQL
// How can I return a relation here with model MyTable?
So I am wondering two things:
How can I use my variables my_limit and my_offset inside the <<-sql? Is my syntax to include those correct?
How can I run this raw query and get a relation out of it?
Thanks!
You can simply use the Rails query language instead of manually building a string with a SQL statement. The following line is identical to your SQL statement and returns an ActiveRecord Relation.
MyModel.limit(my_limit).offset(my_offset).order(:id)

Simple SQL statement in MS Access [duplicate]

This question already has an answer here:
MS Access 2010 SQL Select Into Calculated Column Issue
(1 answer)
Closed 6 years ago.
I'm trying what should be a simple task in Access. Basically to create a new table based on a string matching query. The Hazus_Schools table already exists in my database. The Hazus_Public does not, and I'm trying to create it. The PUBLIC field is a calculated column from another field. The following snippet
SELECT * FROM Hazus_Schools INTO Hazus_Public
WHERE Type = "PUBLIC";
Gives me the following error:
Syntax error in FROM clause
Any ideas what is wrong here?
The order of your INTO and FROM is off, see W3schools Select Into
Try the following:
SELECT *
INTO Hazus_Public
FROM Hazus_Schools
WHERE Type = "PUBLIC"

UPDATE within a table from table name saved in the column [duplicate]

This question already has answers here:
How do I UPDATE from a SELECT in SQL Server?
(38 answers)
Closed 8 years ago.
I have a little problem, but I'm sure it s not really complicated.
It's just hard to find the key word to describe the problem and find a solution
I want to update a column in a table using parameters from this table for a query on an other table.
Example : I have Header + 2 lines
IDSOURCE, IDCIBLE, IDENTIFIANT, TABLE_CIBLE, NOM_ATTRIBUT, NOM_CHAMP_IDENTTIFIANT, NOM_CIBLE
--------------------------------------------------------------------------------------------
DMT_1000, DMT_1000, 1000, [dictionnaire].[dbo].[TABLE_CHAMPS_DATAMART], NOM_CHAMP_DMT, IDENTIFIANT_CHAMP_DATAMART, NULL
DMT_1001, DMT_1001, 1001, [dictionnaire].[dbo].[TABLE_CHAMPS_DATAMART], NOM_CHAMP_DMT, IDENTIFIANT_CHAMP_DATAMART, NULL
And I want to update the last column of each line with something like :
UPDATE
Table
SET
Table.NOM_CIBLE = SELECT table.NOM_ATTRIBUT FROM table.TABLE_CIBLE WHERE table.NOM_CHAMP_IDENTTIFIANT = table.IDCIBLE
FROM
Table
Don't know if it s clear.
Thanks for your help.
This sounds like situation where you could use a cursor. Check out this StackOverflow question How to update a column fetched by a cursor

How do I return a list of all column titles in a table in SQLite? [duplicate]

This question already has answers here:
How can I get the list of a columns in a table for a SQLite database?
(9 answers)
Closed 9 years ago.
I have a 20-or-so column table 'Contacts' in a Contacts.sqlite file. I use the Mac Terminal to interact with it, and am writing a program that will perform certain actions based on the number of columns in the table, as well as the name of each column.
Thus, I need to perform some sort of query that will return a list of all my column titles.
I have found various recommendations that look like some version of this:
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'Schema' AND table_name = 'Table_Name'
but it almost always returns an error saying there is "no such table: information_scheme.columns".
Looks like SQLite does not support information_schema. You can retrieve the list of table names from sqlite_master. But there is no table with columns.
Instead, SQLite supports the proprietary pragma table_info():
pragma table_info(table_name);

'WHERE 1 =1' in SQL Stored Procedure [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?
I've been tasked with reviewing some SQL stored procedures and have seen many that look like the following:
SELECT
X, Y, Z
FROM
Table
WHERE
1 = 1
ORDER BY
X
Why would someone use '1 = 1' for the where clause?
Thanks!
It's common in dynamic SQL, in order to append additional criteria to a WHERE clause. Otherwise, it's useless and it is ignored by the optimizer.
possibly to dynamically add conditions to the where clause.