Trying to retrieve matching columns - mysql

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?

Related

Best practice for handling positions of rows

What is the best practice for moving rows. So that you might want to change order of items. Now if you make a new column called order_id or something, wouldn't that fail if I delete or select rows.
Another method I guess is to just switch values completely with an primary ID, so just values except the ID are changed. however I do not know what people usually use. There are so many websites that give you the ability to change order of things.how so they do that?
Every SQL statement that returns a visible result set should include an ORDER BY clause so that the results are consistent. The Standard does not guarantee that the order of rows in a particular table will remain constant or consistent, even if obvious changes aren't made to the table.
What you use for your ORDER BY clause depends on the use case. A date value is the usual choice for a comment thread or blog entry ordering. However, if you want the user to be able to customize the order that a result set shows in, then you have to provide a column that represents the position of the row, and adjust the value of that column when the user makes changes to the order they see.
For example, if you decide that the column will contain a sequential number, starting with 1 for the first row, 2 for the second, etc. then you will be ok to delete rows when they need to be deleted without having to do updates. However, if you insert a row, you will need to give the row you insert the sequential number appropriate for it's position, and update all rows below that with their new position. Same goes for if you move a row from somewhere else to a new location; the rows between the new and old locations need to be updated with new postion indexes.

MySQL query SELECT as item

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.

Mysql sum as last row

Is it possible to have the SUM of all numaric fields in the last of a set of rows?
As of now, I'm using a very simple query such as:
SELECT
*,
SUM((UNIX_TIMESTAMP(end) - UNIX_TIMESTAMP(start))/3600)
FROM
times
in SQL you cant have a column that appears in only one row, likewise, you also cant have a row that doenst contain all the columns from the other rows.. So having a row that contains something unique is not possible. You can, however, add the calculated column to all rows in the dataset or do the calculation in the calling code after the data is returned.
I think what you are looking for is GROUP BY WITH ROLLUP you will find details on that in the MySQL manual.

Linq-to-Sql Count

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)
{}

Display a column only if it is not null

I need to display a particular column in my SQL result only if it is not null. If it is null, I don't want that column to appear at all in my result.
Is there a way to express this condition in SQL?
This wouldn't make sense because a query may return multiple rows. One row may have a value for the column in question and the next may not have a value. Then a conditional column would create a structural inconsistency between returned rows.
No, it is not...
It's not possible, and really unnecessary. You'll need to have a fixed number of columns, there's just no other way. But that isn't really your problem, you don't want that at all!
Queries are just to retrieve the data, not for the representation of the data. You should just retrieve it and hide the column if all the values are null.
SQL doesn't generally let you reason about properties of entire columns. Conditions are on properties of rows. So there's no way to say "if all the values in this set of this column are null...". However, you can trivially restrict yourself to rows that lack the property.
If you want to show a column only when it is not null for every row, you could do a COUNT(*) WHERE ... your general condition ... AND that_column IS NULL and then redo the query, including the column if the first result was 0 and excluding it otherwise. But I'm not sure why you'd want to do such a thing.