Between but NOT inclusive with sub query - mysql

I have seen replies to this when using dates but not with sub queries. I have the following
SELECT *
FROM `TEST`
where `ID` BETWEEN
(SELECT `ID` FROM `TEST` WHERE `Home_Team`
REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday'
order by ID asc LIMIT 1)
AND
(SELECT `ID` FROM `TEST` WHERE `Home_Team`
REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday'
order by ID asc LIMIT 1,1)
I would like the results not to be inclusive. unfortunately, im not having any luck with any of < > =

Best I'm aware, a BETWEEN b and c is syntactic sugar for b <= a and a <= c, i.e. always inclusive. To make it exclusive, rewrite it as b < a and a < c.

Is ID an integer? If so, just +1 and -1 where appropriate:
SELECT *
FROM `TEST`
where `ID` BETWEEN
(SELECT `ID` FROM `TEST` WHERE `Home_Team`
REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday'
order by ID asc LIMIT 1) + 1
AND
(SELECT `ID` FROM `TEST` WHERE `Home_Team`
REGEXP 'saturday|sunday|monday|tuesday|wednesday|thursday|friday'
order by ID asc LIMIT 1,1) - 1

Related

Set MYSQL WHERE condition from previous SELECT

I have the following MySQL query
SELECT `category`
FROM `jeopardy_questions`
WHERE `amount` = "$2,000"
GROUP BY `category`
HAVING COUNT(*) > 4
ORDER BY RAND() LIMIT 1
This will grab me a random category where there is at least 5 questions in that category.
Now I want to grab all the rows for that category. So how can I do a second SELECT WHERE category is equal to the category returned from the previous query?
I tried the following but I believe the RAND() is causing it to crash/timeout.
SELECT *
FROM `jeopardy_questions`
WHERE `category` = (
SELECT `category`
FROM `jeopardy_questions`
WHERE `amount` = "$2,000"
GROUP BY `category`
HAVING COUNT(*) > 4
ORDER BY RAND() LIMIT 1
)
You can use the above query as a subquery. Something like this:
SELECT *
FROM `jeopardy_questions`
WHERE `category` = (
SELECT `category`
FROM `jeopardy_questions`
WHERE `amount` = "$2,000"
GROUP BY `category`
HAVING COUNT(*) > 4
ORDER BY RAND() LIMIT 1
)

WHERE subquery = 0

I'm trying to write a query which checks if a question have any answers which are marked as a correct answer.
Now I tried using a subquery and I tried setting the subquery as a user-defined variable.
Test #1
SELECT
#id := `id` AS `id`, `title`
FROM
`eu_questions`
WHERE
CAST((SELECT COUNT(`id`) FROM `eu_answers` WHERE `question_id` = #id AND `correct` = 1) AS UNSIGNED) = 0
ORDER BY `id` ASC
LIMIT 0, 10;
Test #2
SELECT
#id := `id` AS `id`,
#count_correct := (SELECT COUNT(`id`) FROM `eu_answers` WHERE `question_id` = #id AND `correct` = 1) AS `count_correct`
FROM
`eu_questions`
WHERE
CAST(#count_correct AS UNSIGNED) = 0
ORDER BY `id` ASC
LIMIT 0, 10;
The queries doesn't result in errors, they simply return 0 results and I don't get why? I tried comparing the user-defined variable while selected and that seems to work and return the correct value, so that simply bugs me even more!
This is how I checked it:
SELECT
#id := `id` AS `id`,
#count_correct := (SELECT COUNT(`id`) FROM `eu_answers` WHERE `question_id` = #id AND `correct` = 1) AS `count_correct`,
CAST(#count_correct AS UNSIGNED) = 0
FROM
`eu_questions`
ORDER BY `id` ASC
LIMIT 0, 10;
Answer - Thanks to Branko Dimitrijevic
I used his example and ended up with this query, which fits my needs.
SELECT *
FROM `eu_questions`
WHERE
1 != ANY (
SELECT COUNT(`id`)
FROM `eu_answers`
WHERE `question_id` = `eu_questions`.`id`
AND `correct` = 1
)
ORDER BY `id` ASC
LIMIT 0, 10;
Here you go:
SELECT *
FROM eu_questions
WHERE 1 = ANY (
SELECT correct
FROM eu_answers
WHERE question_id = eu_questions.id
);
[SQL Fiddle]
Add extra WHERE and LIMIT conditions as desired...
You can LEFT JOIN on the answers table and use a where clause to keep only questions without correct answer.
SELECT
`eu_questions`.`id`, `title`
FROM
`eu_questions`
LEFT JOIN
`eu_answers` ON `question_id` = `eu_questions`.`id` AND `correct` = 1
WHERE
`eu_questions`.`id` IS NULL
ORDER BY `eu_questions`.`id` ASC
LIMIT 0, 10;

Select distinct column

I have the following MYSQL:
SELECT DISTINCT(`user_id`), `type`, `link`, `add_text`
FROM `activity` WHERE `id` != 0 AND `type` !=6 AND (`type` = 4 OR `type` = 5)
ORDER BY `id` DESC LIMIT 4
I only want unique user_ids but I am getting repetitions in my sql results. What is the correct modification to make to this sql so that no repetitions of user_ids
If you don't care about the types, links or add text for each user you could use min/max and group by.
SELECT user_id, max(type), max(link), max(add_text)
FROM activity
WHERE id != 0 A
AND type in (4,5)
Group by User_Id
ORDER BY id DESC LIMIT 4
If you do care you could group_concat the type, link and add text
SELECT user_id, Group_Concat(type), Group_Concat(link), Group_Concat(add_text)
FROM activity
WHERE id != 0 A
AND type in (4,5)
Group by User_Id
ORDER BY id DESC LIMIT 4
edied group by shoudl only be on user_ID
SELECT GROUP_CONCAT(DISTINCT `user_id`) user_ids, `type`, `link`, `add_text`
FROM `activity`
WHERE `id` != 0 AND `type` IN (4,5)
GROUP BY `type`, `link`, `add_text` DESC LIMIT 4;

Mysql query rand() and order by

Hi i'm trying to get some random results ordered by the location ASC.
This is my query:
SELECT `location`, `route`
FROM (`foo`)
WHERE `location` != ''
ORDER BY RAND(), `location` ASC
LIMIT 8
the problem is that it gets randomly but doesn't orders then by "location" ASC, also if i do this:
SELECT `location`, `route`
FROM (`foo`)
WHERE `location` != ''
ORDER BY `location` ASC,RAND()
LIMIT 8
it doesn't gets randomly.
How can i get both togheter RAND() and ORDER BY location ASC ?
You need nested statements/queries:
SELECT *
FROM (
SELECT `location`, `route`
FROM `foo`
WHERE `location` != ''
ORDER BY RAND()
LIMIT 8) AS `temp`
ORDER BY `location` ASC;

re-sorting the output of a SELECT LIMIT query

Fellow coders, i have a table that contains a number of rows each with a date column. I would like to select the last 6 most recent rows. I can do that like this:
SELECT *
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
This returns the rows I need but they are returned in DESC date order. What I want is the last 6 rows in ASC date order. How can I re-sort the output of the SELECT? Any ideas?
thanks
SELECT *
FROM (
SELECT *
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
) s
ORDER BY s.StatsDate
Surround the query in an outer query and order that in a different order.
SELECT * FROM
(
SELECT *
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
) s
ORDER BY `StatsDate` ASC
SELECT *
FROM (
FROM `Stats`
WHERE `ProjectID` = ?
ORDER BY `StatsDate` DESC
LIMIT 6
) as t
ORDER BY t.`StatsDate` ASC;