SQL Returning Count + other columns - mysql

I have the following SQL query : "SELECT * FROM table WHERE id = '$geo_name_id' ORDER BY timestamp DESC LIMIT 10 OFFSET $offset"
This works fine, but now I want to also return the amount of rows in the whole table WHERE id = 'geo_name_id (without the limit). I have tried something like this :
"SELECT COUNT(*) as totalExpectedItems, * FROM story_img_vid WHERE id = '$geo_name_id' ORDER BY timestamp DESC LIMIT 10 OFFSET $offset"
However this doesn't seem to work as expected as it only returns one row.

Use a subquery :
"SELECT t.*,
(SELECT COUNT(*) FROM table
WHERE id = '$geo_name_id') as cnt
FROM table t
WHERE t.id = '$geo_name_id'
ORDER BY t.timestamp DESC
LIMIT 10 OFFSET $offset"
cnt Column will contain the count.
Or a join to a derived table:
"SELECT t.*,
t2.cnt
FROM table t
JOIN(SELECT s.id,count(*) as cnt
FROM table s GROUP BY s.id) t2
ON(t.id = t2.id)
WHERE t.id = '$geo_name_id'
ORDER BY t.timestamp DESC
LIMIT 10 OFFSET $offset"

Do it like this:
"SELECT COUNT(*) as totalExpectedItems, * FROM story_img_vid WHERE id = '$geo_name_id'"

Related

How to use a dynamic limit in sql query

I have a query SELECT * FROM grades WHERE userid = 4123;
I want to limit this query
I have a query SELECT * FROM grades WHERE userid = 4123 LIMIT(2);
This works great but if I want this limit to be dynamic from another query.
SELECT COUNT(id) FROM count_table WHERE course = 131;
doing this gives me a syntax error
SELECT * FROM grades WHERE userid = 4123 LIMIT (SELECT COUNT(id) FROM count_table WHERE course = 131);
if this is not possible at all, then is there an alternative way to achieve this?
please help.!
You can do it in MySQL 8.x using the ROW_NUMBER() function.
Assuming you order the rows by some column (I guessed the column ID... change it as needed), you can do:
select
g.*
from (
select
*,
row_number() over(order by id) as rn -- change ordering as needed
from grades
) g
join (
SELECT COUNT(id) as cnt FROM count_table WHERE course = 131
) c on g.rn <= c.cnt

Check if no results with mysql

I would like to know if there is a way in mysql to check if the query produces some results and if no do execute query. Example:
(SELECT * FROM table WHERE id=2) IF NO RESULT (SELECT * FROM table WHERE id=4)
EDIT
I need only one query, basically first I check if there a result with a param (ex status=0) if there is no result I would like to execute the same query with the param changed to status=2.
Hope it can help
MORE EDIT
Basically I have a table with operatorators and departments and another one with all the users, first I check if there is an available operator in the first table and it's not on holiday, if there is no result I will lselect and admin from the second table, but only if there is no operator availbable
MORE MORE EDIT
This query check if there is an operator available but it doesn't select the admin
query = "SELECT b.id
FROM ".$SupportUserTable." b
INNER JOIN ".$SupportUserPerDepaTable." a
ON b.id=a.user_id
WHERE a.department_id=? AND b.holiday='0' AND a.user_id!=".$_SESSION['id']."
ORDER BY b.assigned_tickets,b.solved_tickets ASC LIMIT 1";
Lastest Solution
This is not exactly what I was looking for, but it works, I'm open to improvments to avoid the execution of two queries:
$query = "SELECT *
FROM(
(SELECT b.id
FROM ".$SupportUserTable." b
INNER JOIN ".$SupportUserPerDepaTable." a
ON b.id=a.user_id
WHERE a.department_id=? AND b.holiday='0' AND a.user_id!=".$_SESSION['id']."
ORDER BY b.assigned_tickets,b.solved_tickets ASC LIMIT 1)
UNION
(SELECT id
FROM ".$SupportUserTable."
WHERE status='2' AND id!=".$_SESSION['id']."
ORDER BY assigned_tickets,solved_tickets ASC LIMIT 1)
) tab
LIMIT 1
";
SELECT * FROM table WHERE id = (
SELECT id FROM table WHERE id IN (2,4) ORDER BY id LIMIT 1
)
You can do like this
Select IF(
(SELECT count(*) FROM table WHERE id=2), (SELECT * FROM table WHERE id=2),(SELECT * FROM table WHERE id=4))
select t.*
from
(SELECT * FROM table
WHERE id in (2,4)
LIMIT 1)t
order by t.id

MySql query ordering

Here is my data. I want to take 6 rows, but I want all HeadlineCategoryId's to be unique in my result list. If I select the top 6 I would take 2 rows from HeadlineCategoryID 20 (6,2). Do you have any suggestions about it?
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT HeadlineCategoryID, MAX(Creation) max_date
FROM TableName
GROUP BY HeadlineCategoryID
) b ON a.HeadlineCategoryID = b.HeadlineCategoryID AND
a.Creation = b.max_date
ORDER BY a.Creation DESC -- << specify here how are you going to sort
LIMIT 6 -- the records you want to get
UPDATE 1
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT HeadlineCategoryID, MAX(NewsID) max_id
FROM TableName
GROUP BY HeadlineCategoryID
) b ON a.HeadlineCategoryID = b.HeadlineCategoryID AND
a.NewsID = b.max_id
ORDER BY a.Creation DESC -- << specify here how are you going to sort
LIMIT 6 -- the records you want to get
It looks like you want the six most recent records, but unique by HeadlineCategoryId. If so, this will work:
select top 6 NewsId, Creation, HeadlineCategoryId
from (select t.*,
row_number() over (partition by HeadlineCategoryId order by Creation desc) as seqnum
from t
) t
where seqnum = 1
As a note . . . This question originally indicated that it was using SQL Server, not MySQL. The solution in MySQL is not as simple. Here is one method with not exists:
select NewsId, Creation, HeadlineCategoryId
from t
where not exists (select 1
from t t2
where t2.HeadlineCategoryId = t.HeadlineCategoryId and
t2.id < t.id)
limit 6
The not exists portion is saying "where there is no other record with a larger id for a given headline category".

How do I select the last but 10 rows from a mysql table

I want to run this query
select *
from table
order asc
limit N;
where N is the total number of rows minus 10.
SELECT COUNT(*) FROM (SELECT * FROM table)
returns the total as 189 so, in this case, I would want my limit to be 179
If order is not important, you can use the offset of limit:
Note, there is no actual value for 'Until End Of Table'. The MySQL Documentation suggests to use "some large number" for the second parameter.
SELECT *
FROM table1
order by ID DESC
LIMIT 10, 999999999999999
If you do want in in ascending order you can apply a different ordering afterwards:
SELECT
*
FROM
(SELECT *
FROM table1
ORDER BY ID DESC
LIMIT 10, 999999999999999) x
ORDER BY
ID ASC
Not sure if the most efficient one, but this should work if you only have one field as a primary key.
select *
from T1
where
T1.id not in (
select top(10) id
from T1
order by id desc
)
order by
id;
It will get the last rows by your order and then you can exclude by the key.
Edit:
Better yet, instead of not in, you can use a left outer join.
select
T1.*
from T1
left outer join ( select top(10) id from Enums_Tables order by id desc ) as T2
on
T1.id = T2.id
where
T2.id is null;

Return Rows Random and then Order By

I have this query that returns the results by ordering it by focus.name ASC. I would like to expand on this by first ordering it by rand() then ordering it by focus.name and limiting it by 10.
SELECT * FROM vendor_products WHERE
vendor_products.focus_id IN (SELECT focus.id FROM focus WHERE
focus.name=? AND
mydelete='0' ORDER BY focus.name ASC) AND product_id=?
AND mydelete='0' ORDER BY focus_id ASC
do something like this
select t1.*
from table t1, (select id from table order by rand() limit 10) t2
where t1.id = T2.id
order by t1.name
see here