The following query returns all records, but they are not in descending order. It seems the query is unaffected by the second parameter of orderBy.
\app\models\Lookup::find()->orderBy('id',SORT_DESC)->all()
You can either do the orderBy like a single string parameter:
\app\models\Lookup::find()->orderBy('id SORT_DESC')->all()
or as an array with key => value:
\app\models\Lookup::find()->orderBy(['id'=>SORT_DESC])->all()
Related
I've written a query that attempts to return a record from a table. It will either return 1 record or nothing.
I've then written a second query and am trying to check whether the first query returned a record or not.
In order to do this I'm trying to use the IsNull function on the primary key of the record in the first query.
I would expect that when the query 1 returns a record, query 2 will return false and when query 1 returns nothing, query 2 will return True.
Instead, when no record is returned by query 1, query 2 returns nothing, just a blank field - why is it not returning True? This is the SQL for query 2:
SELECT IsNull([query 1].fieldA) AS NullorNot
FROM [query 1];
NULL is an undefined column value WITHIN A RECORD. It is still a column value. A query not returning anything has no columns, no values, so has nothing to do with NULL.
I don't think that your on the right track and should rethink your solution (like just testing for the number of records in whatever code you are using the query), but if this is actually what you need, it is this:
SELECT NOT EXISTS(SELECT * FROM [query 1]) AS NullorNot;
OR
SELECT NOT EXISTS([the full SELECT sql of query 1]) AS NullorNot;
I've been playing around with this API and cannot get the $order parameter to work when paired with DESC:
https://data.cityofnewyork.us/resource/9w7m-hzhe.json?$limit=100&$order=score%20DESC
Not only is the data not being sorted accordingly, but the column name/json key (score) I'm attempting to sort against is being omitted from the query results!
According to Socrata docs, you can set to ascending order by replacing the 'DESC' with 'ASC'. The same can be accomplished by not specifying it at all - it will default to ASC. And both of these work fine when I test.
But I can't get DESC to work at all. Thanks.
For some entries, the score column contains nulls, which are sorted first because SQL is weird.
Try adding a filter for $where=score IS NOT NULL:
GET https://data.cityofnewyork.us/resource/9w7m-hzhe.json?$limit=100&$order=score%20DESC&$where=score%20IS%20NOT%20NULL
I am using DLAST to return a specific field value for the last record. The issue I am having is that the last record isn't always the newest date record. I need to return the value of a specific field for the newest date record.
You can't depend on DLast() to return a value from the "last record" of a table unless you use a query based on the table and indicate how the rows should be ordered. From the Application.DLast Method help topic ...
NoteIf you want to return the first or last record in a set of records (a domain), you should create a query sorted as either
ascending or descending and set the TopValues property to 1.
If you want to use DLast(), create a query and use the query name as the domain argument. For example, with this as Query1 ...
SELECT ASSY
FROM L2_AOI1
ORDER BY [your date field];
... this should work as the text box's Control Source ...
=DLast("ASSY", "Query1")
However, you could use a different query which returns the most recent ASSY and use DLookup with that query. For example, with Query2 ...
SELECT TOP 1 ASSY
FROM L2_AOI1
ORDER BY [your date field] DESC;
=DLookup("ASSY", "Query2")
Either way, include an index on [your date field] to optimize performance.
You can also use DLookup directly with an SQL clause:
=DLookup("Assy", "L2_AOI1", "[YourDateField] = (Select Max([YourDateField]) From L2_AOI1)")
Got this QueryBuilder in one of my repositories.
$query = $em->createQueryBuilder('d')
->select('d, i, u, SUM(CASE WHEN t.user = :userId THEN 1 ELSE 0 END) as myTickets')
->leftJoin('d.item','i')
->leftJoin('d.users','u')
->leftJoin('d.tickets','t')
->where('d.active = 1')
->andWhere('d.state = 1')
->setParameter('userId',$user->getId())
->orderBy('d.dateFinish', 'ASC');
When i execute the code, MySQL throws me this error.
Key "premium" for array with keys "0, myTickets" does not exist
"premium" is a field of "d".
How can i recive the fields with the custom SUM?
Since you're using aggregate function in your query, you get the so called mixed result. Mixed result normally return your object fetched with your FROM clause as zero index [0]. The rest of your result is populated based on the aliases you set for your custom fields.
$result[0] will return the object you want to access.
$result['myTickets'] will return the result of your aggregate function. In this case, it's a SUM.
A quote from documentation:
SELECT u, UPPER(u.name) nameUpper FROM MyProject\Model\User u
This query makes use of the UPPER DQL function that returns a scalar value and because there is now a scalar value in the SELECT clause, we get a mixed result.
Conventions for mixed results are as follows:
The object fetched in the FROM clause is always positioned with the key ‘0’.
Every scalar without a name is numbered in the order given in the query, starting with 1.
Every aliased scalar is given with its alias-name as the key. The case of the name is kept.
If several objects are fetched from the FROM clause they alternate every row.
You can read more about this topic here.
I have this below weird problem with my mysql query. Correct me if i am wrong.
This below query( Which i have printed from Codeigniter ) giving me result with subject = 4.
WHERE `notes`.`subject` IN ('4,2') GROUP BY `notes`.`id` ORDER BY `created_date` DESC
But when i changed this
WHERE `notes`.`subject` IN ('2,4') GROUP BY `notes`.`id` ORDER BY `created_date` DESC
It is not returning any result. Why is that?
Lets say i have only one result in table and i am using codeigniter for this.
$this->db->where_in('notes.subject',$this->input->get('subject'));
in should have parameter string like
IN ('2','4'); // for varchar
IN (2,4) // for Integer values
the issue is here
$this->db->where_in('notes.subject',$this->input->get('subject'));
the input variable subject is a string for eg '2,4'
you can use $this->db->where_in('notes.subject',explode(",",$this->input->get('subject')));
which will pass an array to where_in
and as per the User guide
Second parameter to where_in() is an array
Try like this:
WHERE `notes`.`subject` IN ('4','2') ....
for Integers
WHERE `notes`.`subject` IN (4,2) ....