Yii2 Active Record get the latest record - yii2

I have the following table.
As you can see there are duplicate school_id. The id field is auto increment so in order to get the latest, I need to get the row with the highest id per school_id.
Using raw sql, the query is like this,
SELECT
rps_checklist.id, school_id,application_type,school_year
FROM rps_checklist
INNER JOIN (
SELECT id, MAX(id) AS maxx FROM rps_checklist GROUP BY school_id
) ms ON rps_checklist.id = maxx
and the result I need is the following:
P.S I am passing this as searchModel

Related

Subqueries in symfony

I have a Review Entity which contains the fields:
id
userID
bookID
review
and then a Rating Entity which contains the fields:
id
reviewID
userID
rating
The rating could possible store 0 or a 1.
How would I perform a query using symfony to count all ratings from the Rating table that are equal to 1 and then count the ratings that are equal to 0 then minus the count for 0 ratings from the count for 1 ratings and return it for each review?
How would I do that? Seems a very tricky one
Alright as you are using Symfony I assume you are also use Doctrine.
Doctrine allows you to perform Native Queries. Have a look on how to execute a native query here.
On MySQL you may perform Subqueries, have a look here on how to do it.
Then try something like this:
SELECT rv.* , ((SELECT count(*) FROM ratings ra WHERE ra.rating = 1 AND id rv.id = ra.reviewID) - (SELECT count(*) FROM ratings ra WHERE ra.rating = 0 AND id rv.id = ra.reviewID)) as result FROM review rv;
Have a look here on how to get the value attached to the entity.

MYSQL query to Select the first duplicate record in JAVA

I am trying to retrieve the the first row among the duplicate row, THE FIRST OCCURED ***
--Table--
Order_No Product User
1 Book Student
2 Book Student
3 Book Student
I want to get the Order_No of the first duplicate row in JAVA, I have used DISTINCT and DISTINCT TOP 1 etc but nothing worked, NEED HELP
SELECT min(order_no), product, user
FROM 'table'
GROUP BY user, product
This is basic SQL?
SELECT min(order_no), product, user FROM table GROUP BY product, user
See also more information on GROUP BY
All fields not part of your group by must have some sort of way to determine which to pick of the n potentially different values. min() will pick the lowest value (even with strings and dates) while max() will pick the highest. You can also use First() and Last() to grab the value according to when they show up.
Supposing you had other values to pick from, you might see something like:
SELECT min(order_no), product, user, min(creation_date),
sum(quantity), first(billing_address)
FROM orders GROUP BY product, user
SELECT t.*
FROM table t
WHERE NOT EXISTS ( SELECT a
FROM table t2
WHERE t2.Product = t.Product
AND t2.User = t.User
AND t2.Order_No < t.Order_No
)

ORDER BY # of rows with same option ID from different table

I've made a voting script which already works, but I wanted to practice some MySQL to try and do sorting/filtering of the results via SQL queries instead of getting an entire table as an array and working with that using loops.
I've ran into an issue with sorting the options of a vote based on the amount of times it was voted on. My DB has 3 tables related to this script:
vote_votes - which contains the cast votes, one row per vote cast
vote_options - which contains the possible options for every vote
vote_list - which contains the list of votes with title, type etc.
My original script just got all the options that matched the currently visited vote's ID using
SELECT * FROM vote_options WHERE vote_options.vid = $voteID
then counted the rows matching the option's unique ID inside vote_votes in an array, then sorted it based on the amount of rows with a custom sorting function.
I want to do the same in one query, and I think it's possible, I just don't know how. Here's my current SELECT statement:
SELECT
options.optid as id,
options.value as value
FROM vote_options options
WHERE vid = {$vote['vid']};");
Basically, inside vote_votes, each entry has a unique entryid and an optid column, and I want to add it to the query in a way that these entries are counted as WHERE vote_votes.optid = options.optid (Option IDs are unique, so no need to also look for vote ID).
I was hoping this would work, but it's obviously wrong. This is the closest I got before giving up and asking a question here.
SELECT
options.optid as id,
options.value as value
FROM vote_options options
WHERE vid = {$vote['vid']}
ORDER BY (
SELECT COUNT(*)
FROM vote_votes
WHERE vote_votes.optid = options.optid
) DESC;
I've found the solution, it was just a matter of moving the SELECT in brackets to the correct place:
SELECT
options.optid as id,
options.value as value,
options.vid as voteid,
(
SELECT COUNT(*)
FROM vote_votes votes
WHERE votes.optid = options.optid
) as votes
FROM vote_options options
WHERE options.vid = {$vote['vid']}
ORDER BY votes DESC;

How do I make COUNT work how I need it to?

I'm running this query, it works except it doesn't return what I need...
SELECT COUNT(up.profileOwnerUserNumber)
FROM profiles up
INNER JOIN userRatings ur
ON (ur.userRatingTargetUser = up.profileOwnerUserNumber)
WHERE ur.userRatingDateTime>(now()-INTERVAL 1 WEEK)
I get a return of 8 from this query. It's counting all instances found where (ur.userRatingTargetUser = up.profileOwnerUserNumber). But the userRatings table has 4 genuinely different entries in it - the other 4 are duplicates of numbers already found. I want my COUNT to return 4 - the number of distinctly different ur.userRatingTargetUser numbers found, not all 8 entries.
Table userRatings:
userRatingNumber int (autoincrement)
userRatingTargetUser int
Table profiles:
profileNumber int (autoincrement)
profileOwnerUserNumber int
Both userRatingTargetUser and profileOwnerUserNumber have int values that can match because they are set using another table:
Table users:
userNumber int (autoincrement)
How do I change my query so that I no longer count these extra records? SELECT DISTINCT didn't work.
Have you tried GROUP BY:
SELECT COUNT(up.profileOwnerUserNumber)
FROM profiles up
INNER JOIN userRatings ur
ON (ur.userRatingTargetUser = up.profileOwnerUserNumber)
WHERE ur.userRatingDateTime>(now()-INTERVAL 1 WEEK)
GROUP BY up.profileOwnerUserNumber
COUNT is an aggregate function, you should use GROUP BY.
http://dev.mysql.com/doc/refman/5.1/en/group-by-functions.html

mysql: Table alias doesn't exist

So I have a table in my database (which I'll call Test). The table is named Items, and has an int column named itemID and a decimal(8, 2) column named currently. They represent a unique ID for an item and the current bid price on an item, respectively. The currently column is able to be NULL because there may not have been a bid on an item yet.
My goal is to get the itemID of the item with the highest current bid that has at least one bid. I have a mysql query that looks as follows:
select itemID
from (select itemID from Items where numBids > 0) as b
where currently = (select max(currently) from b);
When I execute this in mysql, I get an error that says:
ERROR 1146 (42S02): Table 'Test.b' doesn't exist
Can anyone explain what's going on here? I haven't used mysql in a while so I'm rusty. Do I have to do a join of Items with itself in order to accomplish something like this? Any help would be appreciated.
The alias "b" is not being seen by the last subquery. Insted to use that complicated query use
SELECT itemID
FROM Items
WHERE numBids > 0
ORDER BY currently DESC
LIMIT 1
To handle the situation where multiple items have the highest current bid:
SELECT itemID
FROM Items
WHERE numBids > 0
AND currently =
( SELECT MAX(currently)
FROM Items
WHERE numBids > 0
)
The problem is that you cannot use the b inside the select of the where clausole.