PHP mysql multiple row most recent result [duplicate] - php

This question already has answers here:
SQL select only rows with max value on a column [duplicate]
(27 answers)
Closed 5 years ago.
I have a mysql table that look like this
I'm trying to create a mysql request that will give me, for a specific node, the last available value of each type ( and I have the list of type in another table ).
I really have no idea if its even possible or the right approach to do this kind of task,... i'm not an expert in mysql

I would be inclined to use a correlated subquery in a WHERE clause. I think you are asking for:
select d.*
from data d
where d.date = (select max(d2.date)
from data d2
where d2.node = d.node and d2.type = d.type
);

Related

Insert different content IF SQL table contains rows [duplicate]

This question already has answers here:
How to treat MAX() of an empty table as 0 instead of NULL
(3 answers)
SELECT max(x) is returning null; how can I make it return 0?
(7 answers)
Closed 1 year ago.
I am trying to insert different content based on if a SQL table has Rows. So far the Query will check what the max value of sortingOrder is and then + 1. However this query will break if there is no rows in the table. How can I implement a if statement to check if the table has no rows and then if it doesn't set the sortingOrder to '1'.
INSERT INTO faq (question, answer, sortingOrder)
VALUES ('$questionData', '$answerData', (SELECT MAX(sortingOrder) FROM faq C) +1)
Thanks
The best solution is to make sortingOrder an AUTO_INCREMENT column. The database will assign the values automatically, incrementing them for each row.
If you can't do that for some reason, you can check if the subquery returns NULL and replace it with 1.
INSERT INTO faq (question, answer, sortingOrder)
SELECT '$questionData', '$answerData', IFNULL(MAX(sortingOrder)+1, 1)
FROM faq

Instruction conditional if to checked double records [duplicate]

This question already has answers here:
How to check if a row exists in MySQL? (i.e. check if username or email exists in MySQL)
(4 answers)
Closed 6 years ago.
I have this table:
How I can write instruction if, which will check the records stored at the same time (in my table it is column created_at).
For example:
if(created_at for record where key(it's column)='stan' it is the same as created_at for key='stan_key' {
return records where key='stan'
}
Several options for this. Here's a version using exists:
select *
from yourtable y1
where y1.key = 'stan' and exists (
select 1
from yourtable y2
where y2.key = 'stan_key' and y1.created_at = y2.created_at)

Is there have the best way to count all rows in table mysql? [duplicate]

This question already has answers here:
MySQL: Fastest way to count number of rows
(14 answers)
Closed 8 years ago.
I want to count all of rows in table that match to my condition as fast as mysql can do.
So, i have four SQL and want you to explain all of them, how is it different for each SQL?
and which is fastest or best for query times and server performance? or it has another way that can better than these. Thank you.
select count(*) as total from table where my_condition
select count(1) as total from table where my_condition
select count(primary_key) as total from table where my_condition
or
select * from table where my_condition
//and then use mysql_num_rows()
First of all don't even think about doing the last one! It literally means select all the data I have in the database, return it to the client and the client will count them!
Second, you need to understand that if you're counting by column name, count will not return null values, for example: select count(column1) from table_name; if there is a row where column1 is null, it will not count it, so be careful.
select count(*), select count(1), select count(primary_key) are all equal and you'll see no difference whatsoever.

Get non sequential IDs from MySql [duplicate]

This question already has an answer here:
Find missing sequence gaps mysql [duplicate]
(1 answer)
Closed 8 years ago.
I'm using MySql to keep track of images, each image is assigned an auto increment ID. The ID column should look in theory similar too,
(1,2,3,4,5,6)
Sadly, over the past two years I've been forced to delete rows due to moderation reasons and my ID column looks more along the lines of,
(1,3,4,7)
Is there anyway to get the missing IDs, i.e.:
(2,5,6)
I've search around here and various other forums, but alas I've come up with nothing. The only answer that i've found similar to mine is comparing my ID column to another table's and finding the differences. This is an option, but my main table consists of 25,000 rows so it would be a bit difficult to do.
Thank you in advance!
Create temporary table with integer from 1 to 25000. Then do a left/right join on it to find the missing IDs.
Try that:
SELECT a.id+1 AS start, MIN(b.id) - 1 AS end
FROM table1 AS a, table1 AS b
WHERE a.id < b.id
GROUP BY a.id
HAVING start < MIN(b.id) ;
DEMO HERE

Finding a specific number within a comma separated list [duplicate]

This question already has answers here:
Opposite of MySQL FIND_IN_SET
(6 answers)
Closed 9 years ago.
I have a database which has a field containing some comma separated values like 1,8,3,54,5,19,9..... I want to select only those rows where 2 doesn't exists.
The query below is used for finding all fields containing the number 2 in the attachedCompanyIDs column. However, I want to find all rows where that number doesn't exist, but I don't know how to use find_in_set in this case. Can any one please help me?
SELECT name FROM company
WHERE orderID = 1 AND FIND_IN_SET(2, attachedCompanyIDs);
SELECT name FROM company
WHERE orderID = 1 AND NOT FIND_IN_SET(2, attachedCompanyIDs);
or
SELECT name FROM company
WHERE orderID = 1 AND FIND_IN_SET(2, attachedCompanyIDs) = 0;

Categories