SQL discrepancy using COUNT command - mysql

I am trying to do my first steps with SQL. Currently I am trying to analyse a database and stepped over a problem which I can't explain. Eventually someone could give me a hint.
I have a mySQL table ('cap851312') witch has 330.178 table rows. I already imported the table to Excel, and verified this number!
Every single row includes a field (column 'ID_MES_ANO') for the entries date. For the time being, all the date is uniquely set "201312".
Starting the following command, I would expect to see as a result the given number of rows, however the number which appears is 476.598.
SELECT movedb.cap851312.ID_MES_ANO, count(*)
FROM movedb.cap851312;
I already imported the file to Excel, and verified the number of lines. Indeed, it is 330.178!
How could I find out, what exactly is going wrong?
Update:
I've tried:
SELECT count(*) FROM movedb.cap851312
This returns as well 476.598.
As I am using workbench, I easily could confirm the numer of 330.178 table rows.
Update 2:
The Workbench Table Inspector confirms: "Table rows: 330178"
Solved - However unsure why:
I changed the statement to
SELECT count(ID_MES_ANO) FROM movedb.cap851512;
This time the result is 330178 !

COUNT(*) counts all rows.
COUNT(ID_MES_ANO) counts only rows for which ID_MES_ANO is not null.
So the difference between the two are the rows where ID_MES_ANO is null.
You can verify this with
SELECT count(*) FROM movedb.cap851512 WHERE ID_MES_ANO IS NULL;
By the way:
SELECT movedb.cap851312.ID_MES_ANO, count(*) FROM movedb.cap851312;
means: aggregate all rows to one single result row (by using the aggregate function COUNT without GROUP BY). This result row shows ID_MES_ANO and the number of all records. Standard SQL does not allow this, because you don't tell the DBMS which ID_MES_ANO of those hundreds of thousands of records to show. MySQL violates the standard here and simply picks one ID_MES_ANO arbitrarily from the rows.

Related

Count number of rows using MATCH AGAINST on FullText index column

I have a table in my database that contains quite a lot of data. I have to run a query that should simply count the number of rows where a certain keyword is present in the Message column.
Normally I would run this:
SELECT count(1) FROM applogs WHERE Message LIKE '%level_100%'
This works. It gives me 2972 rows back. But this is obviously very slow for larger datasets.
So I've added a FullText index to the Message column so I can use Match Against like so:
SELECT count(1) FROM applogs WHERE MATCH(Message) AGAINST('level_100');
Now this runs very fast. But this gives me a count of 5672 back.
Because the level_100 term can occur more than once per row. But I'm not interested in how many occurences. I just want to know how many rows.
Is there a way to do this using MATCH AGAINST?

Dynamically Pivoting Table Data | mySQL

Essentially I have a table in my database called Table1 with the following data:
The table has a ProductID that repeats because the values of AssignedColour, ColourFinding and ColourPower vary.
I would like to present all ProductID data in one single row, meaning if there is more than one AssignedColour, ColourFinding and ColourPower listed, it will contain a number at the end.
The final result I of the SELECT query should look like the following:
The number of columns presented horizontally is based on the number of AssignedColour per ProductID
Is something like this possible to accomplish in a mySQL SELECT Query?
An SQL query cannot expand the number of columns of the result set depending on the data values it discovers during query execution. The columns in the SELECT-list must be fixed at the time the query is prepared, before it reads any data.
Also the column names cannot be changed during the query execution. They must be set at the time the query is prepared.
There's no way to do what you are describing in a single SQL query. Your options are:
Do two queries: one to enumerate the colors per product, and then use the result of the first to format a second query with the columns you want.
Do one query to fetch the data in rows as it exists in your table, then write code in your app to display it in rows however you think is best.
Either way, you have to write at least a bit of code in the client. You can't do this in one query.

MySQL "GROUP BY" experiment

I am testing SQL and I am stuck on one query. It is a useless query but I want to understand it.
select count(*), floor(rand()*2) as x from table_name group by x;
The result is either two rows, or duplicate entry '0/1' for key 'group_key'
What happens that leads to this error?
rand() is going to generate a random number for every row in your table. You are then grouping by the results of all of those random numbers. You will get one row for each unique value.
The main point here is not to group by some strange sinthetic data.
Better to group by some certain fields.
Because mysql have some bugs there.
Like this
http://bugs.mysql.com/bug.php?id=58081
Or this
https://bugs.mysql.com/bug.php?id=60808
Certainly it is trying to create a unique index on a tmp table and that is somehow not working

MySQL return different count from select rows, missing data

I recently did the following query in MySQL:
select received,
verified,
accession,
qualityControl,
testMnemonic,
preliminaryReview,
sangerComplete
from Accessions order by accession;
It was missing data, so I looked for a specific row
select received,
verified,
accession,
qualityControl,
testMnemonic,
preliminaryReview,
sangerComplete
from Accessions where accession='14259115189';
The second query return the correct single row for that accession. I went back to the general query, but 14259115189 is not showing up at all. Incidentally, I followed up with a select count(*) from Accessions and it returned a different number of rows from the general query.
Any ideas?

MySQL Error Message: Subquery Returns More than 1 Row

Would you help me to fix this, please:
SELECT
(SELECT AES_DECRYPT(cryptoword, SHA2('DatabaseEncryption1', 512)) FROM file_tree) AS cryptoword1,
(SELECT AES_DECRYPT(name, SHA2(cryptoword1, 512)) FROM file_tree) AS name;
As the topic says, I get error saying that my subquery returns more than 1 row. What I look for to achieve is:
Get the cryptoword for the particular database record.
Use that cryptoword to decrypt the rest of the record.
Repeat the process for all the table records/multiple records satisfying WHERE condition, which I can add later
My query works, if I use the query for one record only. However, I need to get multiple records within from the table. Every record has its own cryptoword, which is different per each row. My task is therefore to get the cryptoword for particular record, and to use that one to decrypt the rest of the record. I need to repeat this process for all the table records.
Because of performance reasons, it all needs to be formatted within one query.
Thank you in advance.
Work out the value of cryptoword1 in a sub-query, then you can reuse the result to work out the value of name in the outer query.
SELECT
cryptoword1,
AES_DECRYPT(name, SHA2(cryptoword1, 512)) AS name
FROM
(
SELECT
AES_DECRYPT(cryptoword, SHA2('DatabaseEncryption1', 512)) AS cryptoword1,
name
FROM
file_tree
)
AS sub_query
Subqueries in the select statement must evaluate to a single value, or else you will get this error. This makes sense since you are looking to fill the value for one field.