SELECT avg(costPrice) as avgCP from table1 where active=1;
This is the sql command to calculate average a column. This is giving the correct answer when I run the command using SQL in phpmyadmin.
But when I run this via PHP it returns wrong answer. Even when there are 2 rows with one active as 0 and one active as 1. It gives me average of both. why so?
Related
We're running the following very simple mysql query through phpmyadmin
SELECT * FROM ProcessedListAssociations
We know the correct result has 751331 rows but successive runs of the query return different row counts - anywhere from 749978 to 752165 rows. At least that's what the row count message at the top of the phpmyadmin result page says:
Showing rows 0 - 24 (752165 total, Query took 0.0005 seconds.)
Running the query from a php script seems to return a result with the correct number of rows.
Running the following query from phpmyadmin:
SELECT count(*) FROM ProcessedListAssociations
also returns the correct result (751331)
We have recreated the table from scratch & still observe the same issue.
The table is an innoDB table. Here's basic info as phpmyAdmin reports it:
Space usage
Data 68.6 MiB
Index 136.3 MiB
Total 204.9 MiB
Row statistics
Format Compact
Collation utf8_general_ci
Next autoindex 751,332
Could it have something to do with concurrency? The server has 4 E7-4870 processors (80 threads total) but in the php.ini thread Safety is disabled.
If that is indeed the problem, then why are we only observing it in phpmyadmin and not with our own php scripts too?
See the answer for incorrect table rowcount in mysql
https://phpmyadmin.readthedocs.io/en/latest/faq.html?highlight=MaxExactCount#the-number-of-rows-for-innodb-tables-is-not-correct
A quick Question. Suppose I have the following two queries:
SELECT TOP 2 * FROM Persons;
and
SELECT * FROM Persons limit 2;
I want to know the difference between the execution of the above 2 queries?
Basically, I want to know when should I use the limit keyword and when it is appropriate to use the top keyword.
Also, How does the database return results based on the above 2 queries.
If you are using SQL Server use TOP.
if you are using MySQL or PostgreSQL use LIMIT!
AFAIK there is no product that currently supports both. Here's one list of current implementations and here's another (covers more products but in less detail)
As stated in my comment for Martin Smith's answer above, there are products that support both, LIMIT and TOP (as you can see here). The difference is that TOP only selects the first n records, but LIMIT allows the definition of an offset to retrieve a specific range of records:
SELECT * FROM ... LIMIT 5 OFFSET 10
This statement selects the 5 records, after skipping 10 records and this isn't possible with TOP.
The example I posted is only checked against the DBS I linked above. I didn't check a SQL standard, because of a lack of time.
TOP & LIMIT both work on amazon Redshift
limit works on MySQL and PostgreSQL, top works on SQL Server, rownum works on Oracle.
There is no difference. The TOP and LIMIT keywords function identically, and will return the same thing.
The DISTINCT command and the TOP command can't work together.
The DISTINCT command and the LIMIT command do work together.
So if you are using DISTINCT you must use LIMIT.
The difference between top and limit is, top only work with single table where as limit can work with join as well
one big mistake, LIMIT is slowly because select is return full and then database server return only limited data. When it is posible used to TOP.
I was running a query in phpmyadmin, it takes around 0.0012 sec to execute.
Showing rows 0 - 29 ( 727,934 total, Query took 0.0012 sec)
may be because of by default limitation in phpmyadmin. But it count the total no of row.
But in php, I need the total no of row .I was running the query without limitation that takes around 6-8 second of time to execute the query.
is their any way to solve this issue.
I have had a similar problem and in my case it was due to the fact that I was not using the correct data types. So while I wanted to query my VarChar Type Index Field with a VarChar I mistakenly formulated the query in such a way that I queried it with and Int Paramter. This resulted in the index not being utilized but rather a full table scan being performed. By example I did:
SELECT * FROM table WHERE Indexfield = 015523;
when I should have done the following:
SELECT * FROM table WHERE Indexfield = '015523';
There is some more insight into the issue here: https://www.percona.com/blog/2006/09/08/why-index-could-refuse-to-work/
I am using codeignitor 2.1.4 and I inserting a bunch of rows using insert_batch. I am connected to SQL Server 2008 database using the SQLSRV driver. The query runs fine but when I run the affected_rows() function I am getting a weird result. Please note that I have made the change in the affected_rows function for the bug so that isn't the problem.
What I am seeing is the returned number being chopped off after 2 digits. So if the inserted rows were say 343 I would get 43. If the affected rows were 35312, I would get 12 returned.
I am wondering if anyone else has seen this type of behaviour? I have checked the return variable type and it is INT so its not that. I am lost as to where the error or change is taking place. Its almost like the return value is being taken as modulus 100 and returned.
So after looking around I found the oversight in the insert_batch and update_batch functions in Codeigniter.
Because the inserts and updates are changed in 100 rows at a time the affected_rows function only sees the last ($rows%100) rows as that would be the last insert/update done.
The solution I found was provided in this link:
Affected rows solution
It doesn't say it in the post there but you need to alter the insert_batch and update_batch functions in the system/database/DB_active_rec.php file.
By running the following SQL command:
SELECT inl_cbsubs_subscriptions.user_id, inl_cbsubs_payment_items.subscription_id,
inl_cbsubs_payment_items.stop_date
FROM inl_cbsubs_subscriptions INNER JOIN
inl_cbsubs_payment_items ON inl_cbsubs_subscriptions.id=inl_cbsubs_payment_items.subscription_id
WHERE inl_cbsubs_subscriptions.user_id=596;
I get the following output:
As you can see, there are a variety of id values that are not always incremental. I need a way to modify the SQL statement so that the search will filter through the results and only provide a single output from the item which has the greatest id value. So, to show what I would like to see from the above example, here is a screenshot:
I am running the SQL statement in a PHP script, so if I need to implement any dynamic variables that would be available. Thanks you for your time.
You can use a DESC sorting on id and limit result with LIMIT 1.