sqlite passing result from a subquery as LIKE argument - sqlite

I have a query where I want to delete all the entries which belongs to a date. I am missing how to pass a LIKE argument from the sub-query. The idea is to match a date from the last entry and delete all the matched entries.
DELETE FROM logentries WHERE datetime(timestamp) LIKE----(SELECT date(timestamp) FROM logentries ORDER BY datetime(timestamp) ASC LIMIT 1);
How to have the above 2 queries in a single query?

Don't use LIKE (there's no pattern matching here), use =:
DELETE FROM logentries WHERE DATE(timestamp) = (SELECT DATE(timestamp) FROM logentries ORDER BY timestamp DESC LIMIT 1);

You must convert your dates to string and it will do the trick.

Related

Queries to be tested in Neo4j

Why I can't get any results with this queries? What I'm doing wrong here?
QUERY 1
MATCH (person:Person)-[:PRS_knows_PRS*1..2]-(friend:Person),
(friend)<-[:CMT_hasCreator_PRS]-(friendPost:Post)-[:PST_hasTag_TAG]->(knownTag:Tag {nameTag:2})
WHERE not(person=friend)
MATCH (friendPost)-[:PST_hasTag_TAG]->(commonTag:Tag)
WHERE not(commonTag=knownTag)
WITH DISTINCT commonTag, knownTag, friend
MATCH (commonTag)<-[:PST_hasTag_TAG]-(commonPost:Post)-[:PST_hasTag_TAG]->(knownTag)
WHERE (commonPost)-[:CMT_hasCreator_PRS]->(friend)
RETURN
commonTag.nameTag AS tagName,
count(commonPost) AS postCount
ORDER BY postCount DESC, tagName ASC
LIMIT 3
QUERY 2
MATCH (person:Person)-[:PRS_knows_PRS*1..2]-(friend:Person)
WHERE not(person=friend)
WITH DISTINCT friend
MATCH (friend)-[worksAt:PRS_worksAt_ORG]->(company:Organisation)-[:ORG_isLocatedIn_PLC]->(:Country {name:{3}})
WHERE worksAt.workFromPWAO < {2}
RETURN
friend.idPerson AS friendId,
friend.firstNamePerson AS friendFirstName,
friend.lastNamePerson AS friendLastName,
worksAt.workFromPWAO AS workFromYear,
company.nameOrganisation AS companyName
ORDER BY workFromYear ASC, friendId ASC, companyName DESC
LIMIT 4
I have this nodes, property Keys and relatioship types:
Could you please help me with this problem?
On your query you use this relationship name:
worksAt:PRS_worksAt_ORG
but on your screen shot of the properties the relationship is called 'PRS_workAt_ORG' without the 's' on workAt.
Maybe that's what's missing on the second query?
Usually it helps to build up the queries step by step.
Then you see where they stop returning data.
A PROFILE (prefix) of your queries should also help to see where it starts to return ZERO rows.
Could also be a case-typo in one of the labels and rel-types.

how to create variable in query sqlite

I want to use variable in my query for sqlite but I don't know how do it?
my query is it :
/*I want to declare my variable in here and use this variable query*/
/*declare vaiable*/SELECT direct_station.iddirect_station,station.name,line.color FROM direct_station JOIN
direction ON direction.iddirection = direct_station.iddirection JOIN
station ON station.idstation = direction.laststation JOIN line ON line.idline = direction.line WHERE direct_station.idstation = /*put variable*/
Explain :
my friends I want execute one query but this query have two part :
first part I want to get several value like this {178,180,200,300}. I got this from this query :
SELECT direct_station.iddirect_station FROM direct_station WHERE direct_station.idstation = 98
and second part I want to use any these values to this query by one variable :
SELECT * FROM stations_view WHERE iddirection = %d
now I want to get any value from second query that joined to first query.
I think that the solution could be to create a Table for your variable where you are going to store the variable's value and from where you can retrieve the value when needed.
INSERT INTO VariableTable (Variable) VALUES ('x');
SELECT * FROM TableName WHERE ColumnName = (SELECT Variable FROM VariableTable);

Delete records which have more than N entries

I have a table called history,
which has three columns.
id, value, timestamp
Id is not a primary key, but the pair (id, timestamp) is unique.
What I would like to do is delete all the older records for a specific ID that exceed a certain limit.
For example if i have these values:
-1,value1,1
-1,value2,2
-1,value3,3
-2,value4,4
-2,value5,5
-2,value6,6
And the limit is 2. After executing the statement i should get something like:
-1,value2,2
-1,value3,3
-2,value4,4
-2,value5,5
-2,value6,6
I think I have it (tried and works for the testcases i had), the answer is:
DELETE FROM history WHERE id = ?1 AND timestamp NOT in (SELECT sourcetime FROM history WHERE id =?1 ORDER BY timestamp DESC LIMIT ?2);
DELETE FROM ... WHERE timestamp < ...
Doesn't work ?

MySQL Changing Order Depending On Contents of a Column

I have a MySQL table Page with 2 columns: PageID and OrderByMethod.
I also then have a Data table with lots of columns including PageID (the Page the data is on), DataName, and DataDate.
I want OrderByMethod to have one of three entries: Most Recent Data First, Most Recent Data Last, and Alphabetically.
Is there a way for me to tack an "ORDER BY" clause to the end of this query that will vary its ordering method based on the contents of the "OrderByMethod" column? For example, in this query, I would want to have the ORDER BY clause contain whatever ordering rule is stored in Page 1's OrderByMethod column.
GET * FROM `Data` WHERE `Data`.`PageID`=1 ORDER BY xxxxxx;
Maybe a SELECT clause in the ORDER BY clause? I'm not sure how that would work though.
Thanks!
select Data.*
from Data
inner join Page on (Data.PageID=Page.PageID)
where Data.PageID=1
order by
if(Page.OrderByMethod='Most Recent Data First', now()-DataDate,
if(Page.OrderByMethod='Most Recent Data Last', DataDate-now(), DataName)
);
You can probably do this with the IF syntax to generate a column that you can then order by.
SELECT *, IF(Page.OrderBy = 'Alphabetically', Data.DataName, IF(Page.OrderBy = 'Most Recent Data First', NOW() - Data.DataDate, Data.DataDate - NOW())) AS OrderColumn
FROM Data
INNER JOIN Page ON Data.PageID = Page.PageID
WHERE Page.PageID = 1
ORDER BY OrderColumn
The direction of the ordering is determined in the calculation of the data instead of specifying a direction in the ORDER BY
Can you just append the order by clause to the select statement and rebind the table on postback?
If you want to use the content of the column in Page table as an expression in ORDER BY you have to do it using prepared statements. Let say, you store in OrderByMethod something like "field1 DESC, field2 ASC" and you want this string to be used as it is:
SET #order_by =(SELECT OrderByMethod FROM Page WHERE id = [value]);
SET #qr = CONCAT(your original query,' ORDER BY ', #order_by);
PREPARE stmt FROM #qr;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
If you want the result set to be sorted based on the value of OrderByMethod , you can use IF as it was already mentioned by others, or CASE :
...
ORDER BY
CASE OrderByMethod
WHEN 'val1' THEN field_name1
WHEN 'val2' THEN field_name2
....etc
END

In query in SQLite

"IN" query is not working. Please guide me if i am wrong.
KaizenResultsInformationTable is MasterTable having field "recordinfo", this field contains Child table Ids as string.
kaizenResultsRecordInformationTable is Childtable having field "recordId".
I have to match records of child.
Query:
select recordinfo from KaizenResultsInformationTable
Output: ;0;1;2;3;4;5;6;7;8;9;10
Query:
select substr(replace(recordinfo,';','","'),3,length(recordinfo))
from KaizenResultsInformationTable`
Output: "0","1","2","3","4","5"
This query is not working:
select * from kaizenResultsRecordInformationTable
where substr(recordid,0,2) in (
select substr(replace(recordinfo,';','","'),3,length(recordinfo))
from KaizenResultsInformationTable
)
This query is working:
select * from kaizenResultsRecordInformationTable
where substr(recordid,0,2) in ("0","1","2","3","4","5")
You can't use in like that. In your second query, you are passing in a single string containing a comma-separated list of values.
It is better to represent a list of IDs as one record for each value.
Also, I'm not sure why you are taking a substring of your recordid. You should usually be storing one value per column.
However, if you can't change the schema, you can use string matching with 'like' instead of 'in'. Something like this should work:
select a.* from kaizenResultsRecordInformationTable a
join KaizenResultsInformationTable b
on (';'+b.recordinfo+';') LIKE ('%;'+trim(substr(recordid,0,2))+';%')
So if your recordinfo looks like 1;2;3;4;5;6, and your substr(recordid,0,2) looks like 1, this will include that row if ";1;2;3;4;5;6;" LIKE "%;1;%", which is true.

Resources