hey guys so i have these 2 lists being pulled out of sqlite3
connection = sqlite3.connect('intel.db')
database = connection.cursor()
top = database.execute("SELECT logtext from logs order by length(logtext) desc limit 10").fetchall()
usr = database.execute("SELECT loguser from logs order by length(logtext) desc limit 10").fetchall()
print(usr,top)
('DIRECT_TEST',), ('ROBOT',), ('ZERO',), ('TEST',), ('Yan Yan',),
('Toasty',), ('!☆♡Fox Wizard♡☆',), ('Agibear',), ('ZEROBIT',), ('Big
Bad Bug',)] [('30000',), ('1605',), ('715',), ('333',), ('209',)
('260',), ('128',), ('376',), ('86',), ('0',)]
how can i make them print like this :
Tiny update.. I changed the logtext from TEXT to INT sorting got
solved.
Try this
for row in database.execute("SELECT logtext, loguser from logs"):
print(row)
Your output is not ordered, so not sure why you need that
But if you did...
SELECT logtext, cast(loguser as INTEGER) loguser from logs ORDER BY loguser DESC
Related
SELECT A.numeClient, A.CNP, A.Adresa, count(B.Eveniment) AS NR_EVE,B.Agent
FROM client A , eveniment B
WHERE A.CNP=B.CNP --AND ROWNUM =1
GROUP BY A.numeClient, A.CNP, A.Adresa,B.Agent
ORDER BY count(B.Eveniment) desc --LIMIT 1;
I want to display the client with the highest amount of events from table eveniment B and the rest of the info. I tried with rownum, but it doesn't work. Also tried with LIMIT 1, but that is out of the window since I need to use Oracle SQL 9. Is there any way around ? I tried using multiple Select, but I don't know how to make it work.
Well, I managed to solve it.
SELECT * FROM
(
SELECT A.numeClient, A.CNP, A.Adresa, count(B.Eveniment) AS NR_EVE,B.Agent
FROM client A , eveniment B
WHERE A.CNP=B.CNP --AND ROWNUM BETWEEN 1 AND 2
GROUP BY A.numeClient, A.CNP, A.Adresa,B.Agent
ORDER BY count(B.Eveniment) desc )
WHERE ROWNUM=1;
I have an Oracle SQL query when run in PROD it gives different results(order) than other environments(DEV,UAT). Below is the sample code:
SELECT xmlelement("MainMenu",
xmlagg(xmlelement("MenuItem",
xmlelement("MenuId", MNI.id),
(SELECT xmlagg(xmlelement("SubMenu", xmlelement("MenuItem",
xmlelement("MenuId", MN2.id),
xmlelement("RoleId", MN2.role_id)
)
)
)
FROM MENU MN2, MROLE RMN2
WHERE MN2.id = RMN2.menu_id
AND MN2.parent_id = MN1.id
AND RMN2.ID = 4
)
)
)
)
FROM MENU MN1, MROLE RMN1
WHERE MN1.id = RMN1.menu_id
AND RMN1.ID = 4;
Below is how I see the results when run in PROD environment.
<MainMenu><MenuItem>
<MenuId>3</MenuId><RoleId>4</RoleId>
<MenuId>2</MenuId><RoleId>4</RoleId>
<MenuId>1</MenuId><RoleId>4</RoleId>
</MenuItem></MainMenu>
Below is how I see the results when ran in DEV & UAT environment.
<MainMenu><MenuItem>
<MenuId>1</MenuId><RoleId>4</RoleId>
<MenuId>2</MenuId><RoleId>4</RoleId>
<MenuId>3</MenuId><RoleId>4</RoleId>
</MenuItem></MainMenu>
The records inserted in the table are in same order but my result set is different and it causing an issue loading the data in the order of MenuID.
The XMLAGG function takes an ORDER BY clause. Try adding:
ORDER BY M2.id
to the end of your XMLAGG.
EDIT:
Will try to explain in more detail this time as best as I can. I tried to make the query a bit simpler thinking it would make it easier to understand but it might have been a bad move.
I'm trying to get the PK_Queue and FK_Queue_Milestone from the 1st row of my Queue table ordered by PriorityScore DESC and TimeAdded ASC
I only want to get the first row, but I was advised to not use TOP(1) because it would result to another SELECT being made to my original select.
This is the query that I have:
SELECT
#Local_PK_Queue = Q.PK_Queue,
#Local_PK_Milestone_Validate = Q.FK_Queue_Milestone
FROM dbo.Queue AS Q
INNER JOIN #Local_PKHolderTable AS P
ON Q.FK_Queue_Process = P.PK_Process
AND Q.FK_Queue_Milestone = P.PK_Milestone
AND Q.FK_Queue_QueueType = P.PK_QueueType
WHERE Q.FK_Queue_Milestone = P.PK_Milestone
AND Q.FK_Queue_Process = P.PK_Process
AND Q.Tags LIKE '%' + #Input_Tags + '%'
AND ((FK_Queue_State = 5 AND TimeDeferred < GETUTCDATE()) OR (FK_Queue_State = 1))
AND Q.FK_Queue_Robot IS NULL
AND Q.FK_Queue_QueueType = P.PK_QueueType
ORDER BY
Q.PriorityScore DESC,
Q.TimeAdded
When I try to run the query, it doesn't seem to be ordering it properly because it always gets the last row of my table.
So did some research and stumbled upon this question here.
It seems to be the same problem that I am experiencing but using MySQL instead of SQL Server.
TLDR: Want to ORDER BY Priority Score DESC and TimeAdded, but is not working properly
Well, you would write this as:
SELECT #var = PK_Test, #var2 = SUM(PriorityScore)
FROM Queue
GROUP BY PK_Test
ORDER BY SUM(PriorityScore);
This is very strange, though, because the GROUP BY presumably returns multiple rows and you presumably want only one. I might suspect that you really want to assign the variables to the highest priority scores:
SELECT TOP (1) #var = PK_Test, #var2 = SUM(PriorityScore)
FROM Queue
GROUP BY PK_Test
ORDER BY SUM(PriorityScore) DESC;
I have just started a website where I show pictures and video. On my FrontPage, I want the latest video/Picture to appear. I upload one Picture or Video each day, so all Pictures and Videos have different dates related to them in my databases.
I have two tables: "Pictures" and "Videos" with the same structure.
The codes I use for fetching data from my databases look like this:
First two codes are the ones that show my Pictures or videos in a list.
$GET_picture = mysql_query("SELECT * FROM Pictures ORDER BY Dato DESC LIMIT 0,1");
while($picture = mysql_fetch_array($GET_picture))
and
$GET_video = mysql_query("SELECT * FROM Videos ORDER BY Dato DESC LIMIT 0,1");
while($video = mysql_fetch_array($GET_film)){
Next two codes are for showing a specific Picture or video:
$GET_spec_picture = mysql_query("SELECT * FROM Pictures WHERE id='$id'");
$spec = mysql_fetch_array($GET_spec_picture);
and
$GET_spec_video = mysql_query("SELECT * FROM Videos WHERE id='$id'");
$spec = mysql_fetch_array($GET_spec_video);
Again, what I want to do is to show the latest (and only the latest) Picture or video on my FrontPage.
I have tried using the UNION tag, but it did not Work. Could anyone show me how to use the tag correctly for this situation, or do I have another problem?
Thank you in advance!
If your PHP really is
if($_GET['mode'] == ""){
$get_content = mysql_fetch_array("
(SELECT * FROM Pictures
ORDER BY Dato DESC LIMIT 0,1)
union all (SELECT * FROM Videos
ORDER BY Dato DESC LIMIT 0,1)
order by Dato DESC LIMIT 0, 1");
while($content = mysql_fetch_array($get_content)){...}
}
then you should replace the first mysql_fetch_array with mysql_query!
Did you try this?
(SELECT * FROM Pictures ORDER BY Dato DESC LIMIT 0,1)
union all
(SELECT * FROM Videos ORDER BY Dato DESC LIMIT 0,1)
order by Date desc
LIMIT 0, 1;
The order by in each subquery is, strictly speaking, unnecessary. However, if you have an index on date, these will quickly get the most recent versions and the final sort on two rows is trivial.
This is the SQL query displayed by codeigniter:
SELECT * FROM (status_updates) JOIN friend_connections ON ((
status_updates.userid=friend_connections.userid) OR
(status_updates.userid=friend_connections.friend_id)) WHERE
status_updates.enabled = "1" AND friend_connections.enabled = "1"
ORDER BY status_updates.date desc, status_updates.time desc LIMIT 20
The Codeigniter commands i used were:
$this->db->from('status_updates');
$this->db->order_by("status_updates.date", "desc");
$this->db->order_by("status_updates.time", "desc");
$this->db->limit(20);
$this->db->join('status_updates', '((status_updates.userid=friend_connections.userid) OR (status_updates.userid=friend_connections.friend_id))');
What i'm trying to do is select everything in two tables, status_updates and friend_connections. status_updates has the one column userid which i want to match with either userid or friend_id from friend_connections. Can someone tell me how i can do this please?
Also i noticed that codeigniter removes the two opening parentheses after the ON command in the above sql query.
Copy of your own answer in order to remove this from the unanswered stack:
$sql = "SELECT * FROM (status_updates) JOIN friend_connections ON ((status_updates.userid=friend_connections.userid) OR (status_updates.userid=friend_connections.friend_id)) WHERE status_updates.enabled = "1" AND friend_connections.enabled = "1" ORDER BY status_updates.date desc, status_updates.time desc LIMIT 20";
$status = $this->db->query($sql);
Please don't upvote. It's not my answer.