mysql php query : why does a including not equal affect the results - mysql

In a mysql query with 5000 records,
in the table: subscriptions [id autoincrement int, name varchar64, archive tinyint4 ]
i'm passing the following sql
$sqlg = "SELECT * FROM subscriptions where archive != 1 order by id desc limit 0,24";
the above returns
records between 3061 - 3012
If i remove the where clause,
SELECT * FROM subscriptions order by id desc
it returns, records between 5066 - 5037 which is the correct result i'm looking for with archive having the value of NULL or 0.
Any ideas why this might be happening?

NULL is not the same as 0. It simply is unknown, so if you want to check for NULL also, use IS NULL
SELECT *
FROM subscriptions
where archive = 0 OR archive IS NULL
order by id desc
limit 0, 24

Related

Is it possible to get the Next and Previous values in an SQL table when you know the Current value?

What I'm trying to do is get the Next and Previous values in SQL.
In this example I'd always know the current value.
This is how my table is laid out.
id
parentID
appID
Name
19410
18371
2da4
name600
19410
18371
4ac0
name24
19410
18371
348e
name441
So my goal is for example get the next/previous value from the current.
So if the current is test2, I'd get test3 back.
I have looked into using offset + limit but I don't think that allows you to select a current starting point.
I cannot store an array as I don't want it to be slow either.
This differs from other questions as I do not have a iterable value as for example name won't always be test1, test2, test3.
My version is 8.0.19 - MySQL Community Server - GPL
For efficiency, you will need INDEX(name) (or some index starting with name)
To get the next row based on "name":
SELECT ...
FROM ...
WHERE name > 'test2'
ORDER BY name ASC
LIMIT 1
For Previous, change 2 things:
SELECT ...
FROM ...
WHERE name < 'test2'
ORDER BY name DESC
LIMIT 1
To get both at the same time:
( SELECT ... FROM ... WHERE name > 'test2' ORDER BY name ASC LIMIT 1 )
UNION ALL
( SELECT ... FROM ... WHERE name < 'test2' ORDER BY name DESC LIMIT 1 )
This will be a lot faster than LEAD and LAG or Cursors.

Update max value from a mysql table

I'm trying to update a table's value using max(col) in a subquery, but for some reason, it's updating all values that match the user_id column I'm using.
This is my table structure:
user_id | steps | in_date
--------+-------+-----------
8 |10 | 1522246892
8 |10 | 1522250713
7 |10 | 1522250799
And this is my query:
UPDATE userdata
SET steps = (steps + 20)
WHERE user_id = 8
AND in_date = (SELECT max(in_date) WHERE user_id = 8);
I expected it to update only the second column, but it instead updates both columns with user_id = 8. Why isn't it working as expected? What am I doing wrong?
Edit: Thanks to Manoj's comment, I changed the query to the following, and it works:
UPDATE userdata
SET steps = (steps + 20)
WHERE user_id = 8
ORDER BY in_date DESC LIMIT 1;
Doing it his way is even better, since I don't have to run two queries, and already gets the highest one by id.
You will encounter sql error "You can't specify target table 'userdata' for update in FROM clause" when you use same table in the sub-query to update the same table.
Its strange that you say its running because,
- you missed from clause in your sub-query
- you can't use same table
Can you please be more specific.
seems you missed the from clause in subselect and for avoid conflit between select and updated value you could build a temp table using ainner subselect
UPDATE userdata
SET steps = (steps + 20)
WHERE user_id = 8
AND in_date = ( select max_date from (
SELECT max(in_date) max_date FROM userdata WHERE user_id = 8) t);
and in this way the AND clause could be always true

WHERE condition and Limit clause will increase the performance or not?

I have the following table
CREATE TABLE `jobs` (
`job_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`job_name` VARCHAR( 255 ) NOT NULL ,
`job_desc` VARCHAR( 255 ) NOT NULL ,
`job_date` DATETIME NOT NULL
) ENGINE = MYISAM ;
Let's assume this table contains 600,000 entries
Question 1: Which one is good to use and possible to increase the query performence?
SELECT job_name FROM jobs WHERE job_id = 10005
Vs
SELECT job_name FROM jobs WHERE job_id = 10005 LIMIT 1
Question 2: What is the drawback if I use this?
SELECT job_name FROM jobs WHERE job_id = 10005 LIMIT 1
To my knowledge, LIMIT helps in paginating data. So, There would definitely be performance differences when the query contains/not contains LIMIT clause.
'LIMIT n' constraints the number of rows returned. The below would return only first 20 rows.
SELECT employee FROM employees WHERE deparment_id = 1 LIMIT 20
But 'LIMIT n,p' paginates the matching records. for example, the below would return the 11th to 16th records that match the WHERE condition.
SELECT employee FROM employees WHERE deparment_id = 1 LIMIT 10,5
So, in cases where the number of rows returned is big, LIMIT would improve performance. However, this might vary depending on the number of rows matching and the pagination range.

My sql Query returning null inspite of having data in the table

I have this query which I am building in my DAO Class after taking the input from te users.Its basic a search functionality with around 10 options users provide..I wrote the below query,
Users give the range of date.
Users provide range of rent.(This part is when it goes as null,I get 0 records.How do I handle null values and still make mysql return data.)
Users select an option to see records which has images.
Status flag for this query should be 1.
Its search by Title.Which means that 1 to 4 points above should be met and keyword given by user should search the column title with like operator.
Finally results are sorted by date.
.
select SQL_CALC_FOUND_ROWS *
from `database`.`table`
WHERE (Date(ctimestamp) BETWEEN '2011-12-02' and '2012-12-06') and (crent BETWEEN '' and '')
and (cimg1 IS NOT NULL or cimg2 IS NOT NULL or cimg3 IS NOT NULL or cimg4 IS NOT NULL or cimg5 IS NOT NULL or cimg6 IS NOT NULL or cimg7 IS NOT NULL or cimg8 IS NOT NULL)
and cflag = 1 and ctitle LIKE '%Testing%'
order by Date(ctimestamp) desc Limit 0, 100
All the parameters are coming from front end.Now for this query I have close to 13 records with same parameters and conditions,but some how when I run it mysql client it gives me 0 records written.
This query gives me the data correctly,
select SQL_CALC_FOUND_ROWS *
from `database`.`table`
WHERE (Date(ctimestamp) BETWEEN '2011-12-02' and '2012-12-06')
and (cimg1 IS NOT NULL or cimg2 IS NOT NULL or cimg3 IS NOT NULL or cimg4 IS NOT NULL or cimg5 IS NOT NULL or cimg6 IS NOT NULL or cimg7 IS NOT NULL or cimg8 IS NOT NULL)
and cflag = 1 and ctitle LIKE '%Testing%'
order by Date(ctimestamp) desc Limit 0, 100
The difference between the two is this part
and (crent BETWEEN '' and '')
Now my doubt is how do I handle this situation when users plans to skip these fields.Appreciate some insights.
crent is likely a numeric value. You're asking whether it's between two text fields. That condition will never be true.
Add validation of the input parameters in your program. Do not put it in the sql when it's NULL.

Why does the total from my query results not add up?

I have three queries that get stats from the database, but the total does not add up correctly for my results. If I do the math myself this is what I get: // 440728 / 1128 = 390.72
However, the following is what is returned by my queries:
SELECT * FROM facebook_accts
WHERE user_id IN (SELECT id FROM `user_accts` WHERE owner_id = '121')
// returns 1128
SELECT sum(friend_count) FROM facebook_accts
WHERE user_id IN
(SELECT id FROM `user_accts` WHERE owner_id = '121')
// returns 440728
SELECT avg(friend_count) FROM facebook_accts
WHERE user_id IN
(SELECT id FROM `user_accts` WHERE owner_id = '121')
// returns 392.11 (number formatted to two decimal places by php)
this may be happening because of column friend_count having some NULL values because SUM and AVG sunctions ignore NULL values. see here.
I guess the 1128 rows contain NULL values (which AVG and SUM ignore).