473,728 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Subquery help

I'm trying to pull the last 10 records from a transactions from a
table using this query:

SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10

But I want to display the rows in ascending order by timestamp. I
can't get the subquery below to work and not sure why:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10)
ORDER BY timestamp ASC;
Please help.
Nov 2 '05 #1
8 1992
>I'm trying to pull the last 10 records from a transactions from a
table using this query:

SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10

But I want to display the rows in ascending order by timestamp. I
can't get the subquery below to work and not sure why:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10)
ORDER BY timestamp ASC;
Please help.
What version of MySQL are you using? Subqueries didn't start working
until about 4.1.something. Also, it doesn't work in 5.0.15 unless you
change it to:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10) a
ORDER BY timestamp ASC;


since it seems to want an alias name for the derived table.

Gordon L. Burditt
Nov 2 '05 #2
The server is running:

PHP Version 4.3.11
--

On Wed, 02 Nov 2005 07:50:27 -0000, go***********@b urditt.org (Gordon
Burditt) wrote:
I'm trying to pull the last 10 records from a transactions from a
table using this query:

SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10

But I want to display the rows in ascending order by timestamp. I
can't get the subquery below to work and not sure why:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10)
ORDER BY timestamp ASC;
Please help.


What version of MySQL are you using? Subqueries didn't start working
until about 4.1.something. Also, it doesn't work in 5.0.15 unless you
change it to:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10) a
ORDER BY timestamp ASC;


since it seems to want an alias name for the derived table.

Gordon L. Burditt


Nov 2 '05 #3
I tried the query with 'a' after ...LIMIT 10) but still doesn't work.

On Wed, 02 Nov 2005 07:50:27 -0000, go***********@b urditt.org (Gordon
Burditt) wrote:
I'm trying to pull the last 10 records from a transactions from a
table using this query:

SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10

But I want to display the rows in ascending order by timestamp. I
can't get the subquery below to work and not sure why:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10)
ORDER BY timestamp ASC;
Please help.


What version of MySQL are you using? Subqueries didn't start working
until about 4.1.something. Also, it doesn't work in 5.0.15 unless you
change it to:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10) a
ORDER BY timestamp ASC;


since it seems to want an alias name for the derived table.

Gordon L. Burditt


Nov 2 '05 #4
>The server is running:

PHP Version 4.3.11
What version of *MYSQL* are you running?

Gordon L. Burditt

--

On Wed, 02 Nov 2005 07:50:27 -0000, go***********@b urditt.org (Gordon
Burditt) wrote:
I'm trying to pull the last 10 records from a transactions from a
table using this query:

SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10

But I want to display the rows in ascending order by timestamp. I
can't get the subquery below to work and not sure why:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10)
ORDER BY timestamp ASC;
Please help.


What version of MySQL are you using? Subqueries didn't start working
until about 4.1.something. Also, it doesn't work in 5.0.15 unless you
change it to:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10) a
ORDER BY timestamp ASC;


since it seems to want an alias name for the derived table.

Gordon L. Burditt

Nov 2 '05 #5
"Neeper" <ne****@hotmail .com> wrote in message
news:t6******** *************** *********@4ax.c om...
I'm trying to pull the last 10 records from a transactions from a
table using this query:

SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10

But I want to display the rows in ascending order by timestamp. I
can't get the subquery below to work and not sure why:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10)
ORDER BY timestamp ASC;


You could do this with a subquery *if available* in your Version of MySQL

SELECT *
FROM (SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10) As X
ORDER BY timestamp ASC;

But the use of temporary tables is equivalent and available further back in
MySQL revision history.

CREATE TEMPORARY TABLE X
SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10;

SELECT * FROM X ORDER BY timestamp ASC;

DROP TABLE X;

These (3) queries produce exactly the same result. I suspect that
subqueries simply use temporary tables behind the scenes anyway. You can
always do it yourself up front as shown.

Thomas Bartkus

Nov 2 '05 #6

"Neeper" <ne****@hotmail .com> wrote in message
news:t6******** *************** *********@4ax.c om...
I'm trying to pull the last 10 records from a transactions from a
table using this query:

SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10

But I want to display the rows in ascending order by timestamp. I
can't get the subquery below to work and not sure why:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10)
ORDER BY timestamp ASC;


You could do this with a subquery *if available* in your Version of MySQL.

SELECT *
FROM (SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10) As X
ORDER BY timestamp ASC;

But the use of temporary tables is equivalent and available further back in
MySQL revision history.

CREATE TEMPORARY TABLE X
SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10;

SELECT * FROM X ORDER BY timestamp ASC;

DROP TABLE X;

These (3) queries produce exactly the same result. I suspect that
subqueries simply use temporary tables behind the scenes anyway. You can
always do it yourself up front as shown.

Thomas Bartkus
Nov 2 '05 #7
I'm using mySQL 4.0.25, I'm not sure if this supports subqueries.


On Wed, 2 Nov 2005 10:08:54 -0600, "Thomas Bartkus"
<th***********@ comcast.net> wrote:

"Neeper" <ne****@hotmail .com> wrote in message
news:t6******* *************** **********@4ax. com...
I'm trying to pull the last 10 records from a transactions from a
table using this query:

SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10

But I want to display the rows in ascending order by timestamp. I
can't get the subquery below to work and not sure why:
SELECT *
FROM (SELECT *
FROM transactions
ORDER BY timestamp DESC
LIMIT 10)
ORDER BY timestamp ASC;


You could do this with a subquery *if available* in your Version of MySQL.

SELECT *
FROM (SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10) As X
ORDER BY timestamp ASC;

But the use of temporary tables is equivalent and available further back in
MySQL revision history.

CREATE TEMPORARY TABLE X
SELECT * FROM transactions ORDER BY timestamp DESC LIMIT 10;

SELECT * FROM X ORDER BY timestamp ASC;

DROP TABLE X;

These (3) queries produce exactly the same result. I suspect that
subqueries simply use temporary tables behind the scenes anyway. You can
always do it yourself up front as shown.

Thomas Bartkus


Nov 2 '05 #8
"Evil Bert" <ne****@hotmail .com> wrote in message
news:a0******** *************** *********@4ax.c om...
I'm using mySQL 4.0.25, I'm not sure if this supports subqueries.


I know that it *does* support the temporary table solution I indicated.
I used this trick all the time with 4.0 in order to work around the lack of
subqueries.

And now that we have subqueries available in our Ver 4.1.5 -
- I find I still prefer to use the temporary tables.

Thomas Bartkus
Nov 2 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1584
by: leegold2 | last post by:
I tried what's below, seemed OK, so I replaced an "IN" for the "=" in the subquery below because of the subquery's error message. I thought w/"IN" I'd get three (3) records returned as expected. But when I tried "IN" I got my entire DB returned - I don't show that below - it kept scrolling so I aborted it. What did I do wrong? Seems like the inner just returns three recs. and should match-up w/the outer for three recs. too. I'm using 4.1....
8
19595
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a couple of tables in my database using INNER JOINS and the WHERE clause to specify the required constraints. However, I also want to read two fields from a *single* record from a table called 'Locations' and then apply one of these field's values...
7
2369
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of which a part may be composed. I have a table of parts and their subparts. The problem is that each of those subparts may be composed of smaller component parts. The subpart would then be listed in the Part field linked to each of its subparts in...
5
6589
by: Rod | last post by:
I have a client site where the code below has been working happily for at least four months. The site is using SQL Server 7. The code is ASP.NET Last week an error appeared related to the following SQL statement. INSERT INTO OrderItems (ClientID, ProductID, OrderHeaderID, Quantity, Dispatched, BackOrdered) SELECT ClientID, ProductID, 1371 AS OrderHeaderID, Quantity, Dispatched, BackOrdered FROM Basket WHERE RequisitionID = 1369 The...
6
34920
by: phillip.s.powell | last post by:
update student s set school_year_id = (select distinct s.id from school_year s, interns i where lower(s.school_year_name) = lower(i.enrollment_year)and s.unique_key = i.unique_key group by s.id); ERROR 1242 (21000): Subquery returns more than 1 row I am trying to replace a column in interns.student.school_year_id to read an ID from the school_year table, where school_year_name will map to interns.interns.enrollment_year I can't for...
2
1939
by: reap76 | last post by:
I am running the following query: if (select IntExternalAccountID from ExternalAccount) = (select IntExternalAccountID from InternalAccount) select * from InternalAccount where AccountPurpose=2 and getting the following error Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. What I am attempting is if the values in ColumnA from...
0
1280
by: fubaba | last post by:
hi, i execute a store procedure got Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.' message is there any one help with these codes below ....... SET @strSQL = (@strSQL + ' AND p.PropertyID NOT IN ( ' + (SELECT DISTINCT a.propertyID FROM propertyAvailability a ...
13
3830
by: ThePrinceIsRight | last post by:
I have a problem with using a subquery in MS Access. The purpose of the sub-query is to create a list of people who have had doctor exams in the past 6 months and exclude them from the main query. This is what I came up with: SELECT GI.generalinfoid AS GeneralInfoId, max(GI.FirstName) AS FirstName, max(GI.LastName) AS LastName, max(TI.ImagingDate) AS ImagingDate, Max(EM.embolizationDate) AS embolizationDate FROM tblImaging AS TI RIGHT JOIN...
1
4166
by: jcf378 | last post by:
Hi all-- Does anyone have any insight as to how I might create a search form that allows a user to select criteria based on any related table in the whole database. The search form I have now only allows me to filter based on variables in a single table. I would like to have a search form where I can select multiple variables (from various linked tables) to filter by, and return results based on this multi-table filter. Allen Browne...
3
11391
by: jideesh | last post by:
---------------trigger code create trigger DeletepurchaseItems on table_purchaseitems for delete,update as begin select * from deleted update table_STOCK set ostock=(ostock-(select sqty from deleted)) where itemcode=(select itemcode from deleted)and coid=(select compid from deleted) and fycode=(select financialyear from deleted) end
0
8894
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9411
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9265
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9188
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9122
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6704
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6012
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4522
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.