I want to join three tables together in order to find a value of the third table's id. The SQL statement works correctly, and looks like this:
SELECT table_three.id from table three
JOIN (table_one JOIN table_two ON table_one.id = table_two.table_one_id)
ON table_three.table_two_id = table_two.id
WHERE table_one.id = x AND table_two.external_id = y AND table three.external_id = z
What would the SQL Alchemy code look like for something like this? I have looked at the documentation, but their query is different than mine, and I can't make sense of the example well enough to apply it to my own code.
Turns out that chaining join statements does exactly what we needed all along.
result = connection.execute(
select(Table_three.id)
.join(Table_two, Table_three.Table_two_id == Table_two.id)
.join(Table_one, Table_two.Table_one_id == Table_one.id)
.where(
and_(
Table_three.external_id == z,
Table_two.external_id == y,
Table_on.id = x
)
)
).one_or_none()
Related
New to doing SQL stuff so please excuse me.
I want to create a SQL query that joins a column to table A from table B based on the following match logic:
B.Source = ‘SOURCE1’ and A.NameCode= B.Code
If the above return NULL then I’d like to match on:
B.Source <> ‘SOURCE1’ and A.UEN = B.UEN**
Any help on how to structure this?
I currently have a union all select query that can get the values based on the above conditions. Should I be using an If/or/case_when in the join process?
A few questions in which I thought could be helpful and that I've looked at are:
How to perform a LEFT JOIN in SQL Server between two SELECT statements?
Using IS NULL or IS NOT NULL on join conditions - Theory question
But I was not able to come up with anything :(
Thank you so much!
Try something like this:
SELECT *
FROM A
JOIN B ON (
(B.Source = 'Source1' AND A.NameCode = B.Code) OR
(B.Source <> 'Source1' AND A.UEN = B.[UEN**]) --Not sure what the ** is? Part of the field name?
)
I have a query such as:
SELECT bp.*, b.company
FROM `DBPREFIXwindows_brands_products` bp
LEFT JOIN `DBPREFIXwindows_brands` b ON bp.brand_id = b.id
JOIN Windows_last_submissions ls
WHERE bp.width = ROUND(ls.width)
AND bp.height = ROUND(ls.height)
AND bp.material = ls.chosenmaterial
AND bp.type = ls.type
AND IF
(ls.minimumbid != 'NULL',bp.cost BETWEEN (ls.minimumbid AND ls.maximumbid),bp.cost <= ls.maximumbid)
the field 'chosenmaterial' comes from a select box which can hold multiple options. My question is, within this query - how do I make the WHERE clause work so that if a person chooses more than 1 option, (which obviously separate into row with commas) how can I query anything which includes any and all of those options??
You can use the IN operator for that field. If you can build a list of chosen materials that matches a pattern like:
(material1, material2, material3)
You can make your where clause something like this:
WHERE bp.material IN (material1, material2, material3)
which will pull any rows that have one of those three materials. In a way, it's just a simplified version of having multiple OR statements:
WHERE material = material1 OR material = material2 OR material = material3
I'm trying to get a search going, where I pass in several different syllables into the 'and_' function via a generator. My search should then return all and only those words that have all given syllables
matches = db.session.query(Word).join(Word.syllables).filter(db.and_(Syllable.syllable==(syl for syl in combo))).all()
This doesn't return anything currently. Does this approach even make sense?
I don't think this query will do what you want. The query you'd generate would be something like this for combo = (1, 2):
SELECT words.* FROM words INNER JOIN syllables ON words.id = syllables.word_id WHERE syllables.syllable = 1 AND syllables.syllable = 2
What I think you want is this for combo = (1, 2):
SELECT words.* FROM words INNER JOIN syllables AS s1 ON words.id = s1.word_id AND s1.syllable = 1 INNER JOIN syllables AS s2 ON words.id = s2.word_id AND s2.syllable = 2
Repeated joining might get slow but here is how you could do this in sqlalchemy:
query = db.session.query(Word)
for syl in combo:
syl_cls = aliased(Syllable)
query = query.join(syl_cls, Word.syllables).filter(syl_cls.syllable == syl)
matches = query.all()
If you were generating OR clauses then this query would be a lot simpler.
I have a MySql db with innoDB tables. Very simplified this is how two tables are layed out:
Table A:
controlID(PK)
controlText
Table B:
controlOptionID(pk)
controlID(FK to table A)
controlOptionType
controlOptionValue
So many controlOptions(table B) can reference one control(giving that control multiple options). But for each option two rows are made in table B: one row with controlOptionType = "linkToCreator" and controlOptionValue = (an ID to the template it was made from*). And the other row type = "optionSelected" and value = "true"(or false).
= its a pretty complicated setup, but basically instead of set columns we are making dynamic ones by means of the type being what the column would have been called. So I couldnt link to the template with FK.
So now I need to select every control(which will have 2 controlOptions linking to it) where the one controlOptionValue value is true or false(depending on what i need) and the other controlOptionValue is an text ID that I specify.
What I think is the best way to do it is a
SELECT * FROM tableB WHERE controlOptionType = 'linkToCreator'
Then do a loop over that result set saying:
SELECT * FROM tableB WHERE tableB.controlID = (the controlID in this iterations row) AND tableB.controlValue = 'true'
But maybe thatls really inefficient, and either way I have no clue how to do that. It would be great if I could get a single query(i.e. not using stored procedures) that I specified templateID and true or false and it gave me a row result if it didn't find anything.
BTW this is for a search in our application with will need to go through TONS of rows so performance is paramount. And yes, I know the setup isnt the greatest...
Thanks :D
Like this?
SELECT * FROM tableA AS A
LEFT JOIN tableB AS ctrl1 ON (A.controlID = ctrl1.controlID AND ctrl1.controlOptionType = ? AND ctrl1.controlOptionValue = ?)
LEFT JOIN tableB AS ctrl2 ON (A.controlID = ctrl2.controlID AND ctrl2.controlOptionType = ? AND ctrl2.controlOptionValue = ?)
Try this:
SELECT *
FROM Table_A
LEFT JOIN Table_B
ON Table_A.ControlID = Table_B.ControlID
WHERE Table_A.controlOptionType = 'linkToCreator
Given a database with two tables X and Y, I have a query that should LEFT JOIN the two tables on attributes X.a1 and Y.b1. I used the following query:
SELECT X.a1, X.a2, Y.b1, Y.b2 FROM X LEFT JOIN Y ON (X.a1 = Y.b1)
I thought that would be good enough to work, even if Y is currently an empty table. However, the query breaks because table Y is empty, it seems. Is there any way to reformat this query so that even if Y is an empty table, the LEFT JOIN will not break? Or do I just need to always make sure that there is some data in table Y, even if it doesn't match anything in table X (hence the LEFT JOIN).
Since you didn't post your actual SQL, i just make assumption here. My experience telling me that you might have a where clause that causes the SQL to return empty set.
SELECT X.a1, X.a2, Y.b1, Y.b2 FROM X LEFT JOIN Y ON (X.a1 = Y.b1)
WHERE Y.b3 = 'something'
The above SQL will return empty result set. You may need to modify your SQL into the following format, by bring up the problematic where clause to LEFT JOIN ON clause.
SELECT X.a1, X.a2, Y.b1, Y.b2 FROM X
LEFT JOIN Y ON (X.a1 = Y.b1 and Y.b3 = 'something')
Your table names are a little confusing. Is it X and Y, or X.a and Y.b?
If X and Y:
SELECT X.a1, X.a2, Y.a1, Y.b2 FROM X LEFT OUTER JOIN Y ON (X.a1 = Y.b1)
should bring back all X, with nulls for the Y.a1 and Y.b2 where there is no matching record.
Try your query on some Sql editor that returns errors like
HeidiSQL or similar. In my case the problem was ambiguous id in WHERE clause.