I was fiddling with some mySQL I had written a while ago and I was wondering how to make the queries more efficient. After doing some research I came across a website that said
SELECT (SELECT Value FROM table WHERE (variable1 == "answer" AND variable2 = "answer2")) AS variable;
could be used, in theory, to return either NULL if nothing was found or the value if the criteria were met.
Having tried this, I could not get it to work. Could someone tell me if either:
1. I'm doing this stupidly and there is a more efficient system for doing this (that is not count)
2. It works, but what I'm doing wrong with it
If you want to get a a single value if it exists, otherwise NULL (instead of an empty result set), this approach is as efficient as it will get. The time it takes will be the same as the time it takes to run the inner query alone.
The only problem is that the if inner query returns more than one row you will get an error. You can fix that by adding LIMIT 1 or making sure it never returns more than one row.
Related
I am running a join on 3 large tables (over a hundred thousand rows each). The query returns 4 rows as expected, but one of the rows has a "NULL" value, yet in the table is a value I expect to see. I am sure the value is in the db because I can see it in the table, but for some reason, MySQL is returning everything except that particular value. I am not getting any errors. The query runs exactly as expected, except for this null value. See the screenshot below:
My Question is why would this happen? Has anybody experienced this? Could it be a bug in phpmyadmin? This query is supposed to be a report of some transactions, so you can imagine how funny the report is looking with a blank field that cannot be explained!
have you tried doing it with the inner join (clasic one) , when you do a left join it brings all rows from the left table and those who did not match have null values.
here they explain it better and you can see examples
https://www.w3schools.com/sql/sql_join.asp
I have a query in a base-data table that (given that my search criteria are correct) gives back approx. 950 records.
Except of the 3 criteria fields, i want to have about 10 more fields (the Project is still at the beginning) , every single one based on sub-queries, some of them normal select queries, some are aggregated queries.
As far as i know every sub-query must give 1 and only one value back.
This value school be individual for every Record of the top query.
My Problem now is, that i don't know how to pass the search criteria from the top query (simple select query) to the sub-query in the in 10 fields i mentioned before.
Is this possible at all, or is my Approach to complicated. Is there possibly an easier way?
I have a Windows 7 System with Office 2010 installed.
Your help is much appreciated.
Thanks a lot.
PS
The sub-queries are based on the same table as the top query. Sorry, I forgot to mention.
You can pass arguments between things with a function call to set a public variable. This vba must be in a Module, not behind a Form Module. I don't use this approach very often, because the global value is in volatile memory, I prefer to save the variable in a special data Table.
Public strGlobal As String
Function Func_ReadGlobal() As String
Func_ReadGlobal = strGlobal
End Function
Function Func_WriteGlobal() As String
strGlobal = Func_WriteGlobal
End Function
In all subqueries create parameter(s) and use it as search criteria. Parameter name should be the same for same column. Now, if you use those subqueries in your main query, Access will ask only once per each parameter name, you don't need to pass them explicitly to subqueries.
Thank you guys.
I did'nt think of the most obvious solution with the Globals. I will try it out as soon as my Boss gives me the time to continue with the Project.
#Sergey
I can't use the Parameter(s) way, because the whole query, incl. Subqueries shall run completely alone in VBA, without human input at all.
I have a table, i'm interested in 2 columns. table.ACTUAL_IDENTIFIER and MODEL_IDENTIFIER.
I need to ensure that ACTUAL_IDENTIFIER is set to the VALUE OF MODEL_IDENTIFIER.
My way of thinking is going to the table and taking a note of the row count. Then running sql to return all rows which ACTUAL_IDENTIFIER = MODEL_IDENTIFIER and ensuring both row counts add up, if not it's clear ACTUAL_IDENTIFIER != MODEL_IDENTIFIER for all rows and there are errors somewhere.
Can someone help me out with the sql i will need to use to accomplish this?
I have the following simple MySQL query, called from PHP:
SELECT foo_id, SUM(number_of_guests)
FROM guests
WHERE foo_id = $foo_id
GROUP BY foo_id
This works fine, except for one $foo_id, which returns about 2.5 times greater than the sum of the number_of_guests field.
What could cause this behavior for only a certain value of $foo_id?
Is there a better way to do this?
Your query should work. The error is most likely in the other method you are using to verify the result.
Is there a better way to do this?
Yes. Since you are only fetching one group there's no need for your GROUP BY clause:
SELECT SUM(number_of_guests)
FROM guests
WHERE foo_id = $foo_id
The problem is that there was one row with a large value for number_of_guests. I didn't see in in browsing the data because there are a few hundred rows. It didn't show up when I copied and pasted from HTML page into Excel because that row was missing most of the other columns, and the HTML page has all the columns.
Thanks for all your help!
I need to do a count on the items in a joined result set where a condition is true. I thus have a "from join where where" type of expression. This expression must end with a select or groupby. I do not need the column data actually and figure it is thus faster not to select it:
count = (from e in dc.entries select new {}).Count();
I have 2 questions:
Is there a faster way to do this in terms of the db load?
I have to duplicate my entire copy of the query. Is there a way to structure my query where I can have it one place for both counts and for getting say a list with all fields?
Thanks.
Please pay especial attention:
The query is a join and not a simple table thus I must use a select statement.
I will need 2 different query bodies because I do not need to load all the actual fields for the count but will for the list.
I assume when I use the select query it is filling up with data when I use query.Count vs Table.Count. Look forward to those who understand what I'm asking for possible better ways to do this and some detailed knowledge of what actually happens. I need to pull out the logging to look into this deeper.
Queryable.Count
The query behavior that occurs as a
result of executing an expression tree
that represents calling
Count(IQueryable)
depends on the implementation of the
type of the source parameter. The
expected behavior is that it counts
the number of items in source.
In fact, if you use LinqToSql or LinqToEntities, Queryable.Count() is sent into the database. No columns are loaded to memory. Check the generated sql to confirm.
I assume when I use the select query it is filling up with data when I use query.Count vs Table.Count
This is not true. Check the generated sql to confirm.
I have to duplicate my entire copy of the query. Is there a way to structure my query where I can have it one place for both counts and for getting say a list with all fields
If you need both the count and the list, get the list and count it.
If you need the count sometimes and other times you need the list... write a method that returns the complex IQueryable, and sometimes call .Count() and other times call .ToList();
I do not need the column data actually and figure it is thus faster not to select it.
This is basically false in your scenario. It can be true in a scenario where an index covers the result columns, but you don't have any result columns.
In your scenario, whatever index is chosen by the query optimizer, that index can be used to make the count.
Sum up: Query optimizer will perform the optimization you desire.
//you can put a where condition here
var queryEntries = from e in dc.entries select e;
//Get count
queryEntries.Count();
//Loop through Entries, so you basically returned all entries
foreach(entry en in queryEntries)
{}