i guess that i ask a kind of question like it was ask a thousand times, before, but i dont understand the part in other questions, i hope someone could explain it me at my simple code.
I have two tables
TableA -> ID|SITEID|NEXT|...
TABLEB -> ID|SITEID|ANOTHER|...
Now i want to catch all results wich are matched by the same SITEID='SITEXY' and TABLEB.ANOTHER='IDXY'. As result i only want to recieve the fields of TABLEA.
At the moment i do it this way, but i get the fields from both tables.
SELECT * FROM TABLEA, TABLEB WHERE TABLEA.SITEID='SITEXY' AND TABLEB.ANOTHER='IDXY' AND TABLEA.SITEID=TABLEB.SITEID;
Mybe its better to use "USING" or "JOIN" but i'm to stupid to understand how it works....:-(
You can qualify the wildcard with the table from which you want to get the rows:
select TABLEA.*
from TABLEA
join TABLEB on TABLEA.SITEID = TABLEB.SITEID
where TABLEA.SITEID = 'SITEXY'
and TABLEB.ANOTHER = 'IDXY';
Also, always use modern explicit join syntax instead of comma based join.
Using aliases, you can make the query bit cleaner:
select a.*
from TABLEA a
join TABLEB b on a.SITEID = b.SITEID
where a.SITEID = 'SITEXY'
and b.ANOTHER = 'IDXY';
Assign aliases to both tables, and then select all columns from TABLEA:
SELECT a.*
FROM TABLEA a
INNER JOIN TABLEB b
ON a.SITEID = b.SITEID
WHERE a.SITEID = 'SITEXY' AND
b.ANOTHER = 'IDXY';
Aliases make it easier to read and write a query. Note that I have also replaced your implicit join with an explicit one using INNER JOIN and ON. As a general rule, you should avoid writing commas in the FROM clause.
Related
I am trying to left join two tables (A and B) together and would like to return all values that have a column in table B marked as null in mySQL.
The two tables I am joining are both very large and I am running into an issue where my connection will timeout after 6000 seconds due to the DBMS settings; is there a way to make this query run more efficiently?
Another bit of information: Even if I limit the query to 10 rows, it will still timeout and give me the error code listed below.
select *
from Table_A a
left join Table_B b
on a.field_X = b.field_X
where b.Field_X is null;
I am experiencing the following error code: "Error Code 2013. Lost Connection to MySQL server during query."
Side note: I am a new SQL user and may need to ask for clarification on some answers. Thank you in advance!
First, you only need columns from a because the b columns are all null. You can write this as:
select a.*
from Table_A a left join
Table_B b
on a.field_X = b.field_X
where b.Field_X is null;
Then for this query, I recommend an index on table_b(field_x).
Maybe something like this will work for you:
select * from table_A a where a.field_X not in (select field_x from table_B);
Try the other way around, and reduce your SELECT fields if you can:
select Table_B.Field_X
from Table_B b
left join Table_A a
on a.field_X = b.field_X
where a.Field_X is null;
Let SQL do its work they way it likes to do it :) To have a standard left join (where you are looking for rows that do NOT match on the right) is highly optimised if you have the correct indexes
I want to execute value from two tables. I have write query to execute value but i don't know is it wrong or true. I provide in following in query.
"SELECT a.id,a.name,b.address,b.pin FROM table1 a,table2 b WHERE a.id=b.id";
You want to JOIN the two tables. You are trying with an implicit JOIN notation that is deprecated and you should do it with an explicit JOIN like this:
SELECT a.id,a.name,b.address,b.pin
FROM table1 a JOIN table2 b ON a.id=b.id
This is untested since you didn't provide examples of your data but you can read it as:
Select id and name from table a, address and pin from table b joining
them on the id field of each that must match. Swow only those records
that match.
You can read more here
I have some tables:
tableA (cola1, cola2, cola3, cola4, cola5)
tableB (colb1, colb2)
tableC (colc1, colc2, colc3)
tableA.cola2 refers to tableB.colb1
and tableA.cola3 refers to tableC.colc1
I want to retrieve data from all those tables, I have a query using join like this:
Select tableA.cola1, tableA.cola2, tableA.cola3, tableA.cola4, tableA.cola5, tableB.colb2, tableC.colc2, tableC.colc3
FROM tableA
INNER JOIN tableB ON tableA.cola2 = tableB.colb1
INNER JOIN tableC ON tableA.cola3 = tableC.colc1
WHERE tableA.cola5 = 'something'
So, is it possible to write this query using subqueries instead of JOIN?
and what would be better? subqueries or JOIN?
A friend of mine told me that when you have large tables, JOIN is slow and requires a powerful computer, while subqueries is faster and doens't require a powerful computer to perform the selection. He said it's because subqueries return results based on something like addition, and JOIN reutrn results based on something like multiplication (I'm not good at English so I don't know how to put this, but hope you get the idea). I am new to this and I've tried to google but still can't understand that. Would anybody please spare sometime answer my question and explain this subquery vs JOIN thing to me? Thank you very much.
In most cases JOINs are faster than sub-queries and it is very rare for a sub-query to be faster.
JOINs RDBMS can create an execution plan that is better for your query and can predict what data should be loaded to be processed and save time, unlike the sub-query where it will run all the queries and load all their data to do the processing.
Do it this way:
Select cola1, cola2, cola3, cola4, cola5, colb2, colc2, colc3,colb1, colb2,colc1, colc2, colc3 FROM
(Select cola1, cola2, cola3, cola4, cola5, colb2, colc2, colc3,colb1, colb2 FROM
(Select cola1, cola2, cola3, cola4, cola5, colb2, colc2, colc3 FROM tableA) as A
INNER JOIN
(Select colb1, colb2 from tableB) as B
ON A.cola2 = B.colb1)as AB
INNER JOIN
(Select colc1, colc2, colc3 From tableC) as C
ON AB.cola3 = C.colc1
WHERE AB.cola5 = 'something'
We have been doing queries a bunch of different ways and queries have been working when we do a
SELECT t.thing FROM table1 t JOIN table2 s WHERE t.something = s.somethingelse AND t.something = 1
and it worked with all queries except one. This one query was hanging forever and crashes our server, but it apparently works if we do it like:
SELECT t.thing FROM table1 t JOIN table2 s ON t.something = s.somethingelse WHERE t.something = 1
We are trying to figure out if the problem is due to the query structure or due to some corruption in the account we are trying to query.
Is the first syntax correct? Thanks.
You need to use the ON clause. Though you can also join with commas, e.g.: SELECT * FROM table1, table2;
Hope that helps!
There are different ANSI formats.. you can use
Select ...
from tbl1 join tbl2 on tbl1.fld = tbl2.fld
OR
select ...
from tbl1, tbl2
where tbl1.fld = tbl2.fld...
The explicit join is the more common format where you are explicitly showing developers after yourself how the tables are related without respect to filtering criteria.
Your first syntax miss the ON. When you join it is mandatory to tell ON what fields the join is happening.
I would recommend using JOIN ON over WHERE to do your joins.
1) your where clause will be easier to read since it will not be pollute by join where clause.
2) your join section is easier to read and understand.
We all agree both method works, but the JOIN one is better due to theses points.
my 2 cents
As title says, this issue happens in MS Access 2003 SP1. Does anyone know what could be solution for this problem?
Pseudo query
select * from a inner join b on a.id=b.id
For small data sets, there are any number of approaches using various possible string conversions.
But if your data sets are of any size at all, this will be very slow because it can't use the indexes.
You could possibly optimize by joining case insensitively and then using criteria to test whether the case is the same, e.g.:
SELECT *
FROM a INNER JOIN b ON a.id=b.id
WHERE Asc(a.id) <> Asc(b.id)
This would at least allow the use of an index join so you wouldn't be comparing "a" to "b" and "a" to "c" (as would be the case with joining on string functions), but only "a" to "a" and "a" to "A".
I would suggest that if your data really needs to distinguish case, then you probably need to store it in a database engine that can distinguish case in joins and then pass your SQL off from Access to that database engine (with a passthrough query, for example).
EDIT:
#apenwarr suggests using StrComp() in the JOIN (as did #butterchicken yesterday), and this SQL raises a question for me (I've updated her/his SQL to use the same table and fieldnames I use above; it's essentially the same as #butterchicken's SQL)
SELECT *
FROM a INNER JOIN b
ON a.id = b.id
AND StrComp(a.id, b.id, 0) = 0
It is a fact that Jet will optimize a JOIN on an index exactly the same way it would optimize the equivalent WHERE clause (i.e., implicit JOIN). Stripped down to just the JOIN (presumably on indexed fields), these two SQL statements will be optimized identically by Jet:
SELECT *
FROM a INNER JOIN a
ON a.id = b.id
SELECT *
FROM a, b
WHERE a.id = b.id
My question is whether or not these three will optimize identically:
SELECT *
FROM a INNER JOIN b
ON a.id = b.id
AND StrComp(a.id, b.id, 0) = 0
SELECT *
FROM a INNER JOIN b
ON a.id = b.id
WHERE StrComp(a.id, b.id, 0) = 0
SELECT *
FROM a, b
WHERE a.id = b.id
AND StrComp(a.id, b.id, 0) = 0
I'm using SO to avoid work I'm supposed to do for tomorrow, so don't have time to create a sample database and set up SHOWPLAN to test this, but the OP should definitely give it a try and report back on the results (assuming he/she is definitely intending to do this with Jet).
Have you tried StrComp? Not an Access-bod, but I think the query would look something like this:
SELECT * FROM MyTable A INNER JOIN MyTable B
on StrComp(A.description,B.description, 0) = 0
The 0 argument in StrComp causes a binary compare which will catch differences between "You" and "you" (for example).
I believe that SQL in Access is not case sensitive. Try this Kb article for a possible solution.
Here's another possible solution. As mentioned in the posting, you are sacrificing speed...
Here's a variation of the earlier suggestion to use StrComp, but this one will start by narrowing down the join using indexes, then restrict it further using StrComp.
SELECT * FROM MyTable A INNER JOIN MyTable B
on A.description = B.description
AND StrComp(A.description,B.description, 0) = 0
This should be fast even on large data sets, as long as you don't have lots of entries that are distinguished only by case.