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
Related
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
Having the following tables
Post(*id, name, description, cat, publish_date)
Category(*id, name)
It is possible in ONE query to get (max) the first N element of each different category?
Assuming that N=3, i'd need the following result:
Result set:
["1", "Name1","Descr","cat1"]
["2", "Name1","Descr","cat1"]
["3", "Name1","Descr","cat1"]
["10","Name1","Descr","cat2"]
["20","Name1","Descr","cat2"]
["22","Name1","Descr","cat2"]
["25","Name1","Descr","cat3"]
["30","Name1","Descr","cat3"]
["19","Name1","Descr","cat3"]
And so on.
I need this, to get the first N article of EACH category, with one query (so without ask for a specific category but for all category in table)
It is possible? If yes what's the right query?
This query will do what you need. If any category has less than 3 post it will still work.
SELECT P.id,P.name,P.description,C.name
FROM Post P
LEFT JOIN Category C
ON P.type = C.id
WHERE FIND_IN_SET(P.id,
(
SELECT GROUP_CONCAT(ids) FROM
(SELECT SUBSTRING_INDEX(GROUP_CONCAT(id),',',3) as ids
FROM Post
GROUP BY type
) AS foo
GROUP BY ''
)
)
Here is a working SQL Fiddle
UPDATE
In response to your comment and updated question:
SELECT P.id,P.name,P.description,P.publish_date,C.name
FROM Post P
LEFT JOIN Category C
ON P.type = C.id
WHERE FIND_IN_SET(P.id,
(
SELECT GROUP_CONCAT(ids) FROM
(SELECT SUBSTRING_INDEX(GROUP_CONCAT(id ORDER BY publish_date DESC),',',3) as ids
FROM Post
GROUP BY type
) AS foo
GROUP BY ''
)
)
You can use UNION to join multiple queries into one. This assumes that you know what type you are selecting for each set.
SELECT * FROM
(
SELECT * FROM T1 WHERE type='Type1' ORDER BY id DESC LIMIT 3
) DUMMY1
UNION ALL
SELECT * FROM
(
SELECT * FROM T1 WHERE type='Type2' ORDER BY id DESC LIMIT 3
) DUMMY2
UNION ALL
SELECT * FROM
(
SELECT * FROM T1 WHERE type='Type3' ORDER BY id DESC LIMIT 3
) DUMMY3
The DUMMY table aliases are needed to allow ordering within each subquery.
I would like to get all the rows from the two users with the greatest number of rows, that is, the two users with the greatest activity in a log table.
I have only found next solution: first, get the number of rows for every user, an limit it to 2:
SELECT userid, count(*) AS n_of_rows FROM my_table GROUP BY userid LIMIT 2;
Then, from the source code I'm querying the database (Python for example), query the database to get the rows of each user:
SELECT * FROM my_table where userid = $userid
Is it the best/elegant solution, taking into account SQL language itself and database performance?
Thanks!
I think what you're looking for is something like
select * from my_table where userid in
(select userid from my_table
group by userid
order by count(*) desc
limit 2)
To get the rows and keep the order, use a join with aggregation:
select t.*
from my_table t join
(select userid, count(*) as cnt
from my_table
group by userid
order by count(*) desc
limit 2
) top2
on t.userid = top2.userid
order by top2.cnt desc, userid;
Try this:
SELECT TOP 2 userid, count(*) AS n_of_rows
FROM my_table
GROUP BY userid
ORDER BY count(*) desc
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".
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;