I'm trying to build a query with a variable limit. As far as I know I cannot do something like select * from table limit my_variable;, but I've read on the internet about a workaround:
SET SQL_SELECT_LIMIT = variable;
I have to write this syntax before the query I want to apply the LIMIT in. This works fine if I write a SELECT query after that code line, but it does not work if I write the following code instead:
INSERT INTO table
SELECT * FROM table1;
It will insert every record of the table1 in table2, instead of inserting the quantity of records specified in the first code line I wrote in this post.
How can I accomplish this?
Using prepared statements to dynamically compose SQL queries.
Related
I have a history table that I am trying to insert data into. The data in the source table is horizontal i need the data vertical. In order to do this, I run 36 queries. I would like to run 1 query that has 36 select statements in it. I wrote a select query that has a union it it. it runs fine and i get the data. when i take that query and convert it to an insert into query, i get an error at the UNION clause
Anyone run into this?
Save your working union select query as a query.
Then use this query as source in a new append query which will append records to your table.
Can we write this query without using sub-query?
select * from test where EmpTran in
(select max(EmpTran) from test);
I tried this code but it returns empty set.
I read that, 'in absence of group by, entire data is taken as a single group', if that's the case the the query should return same result as the query above.
select EmpTran,EmpName from test
having EmpTran = max(EmpTran);
Sample data:
create table test(EmpName varchar(10),EmpTrans int);
insert into test values('Ans',100);
insert into test values('Sam',50);
insert into test values('Kar',150);
insert into test values('Sar',200);
insert into test values('Raj',200);
The second query doesn't work because as soon as you use an aggregation function anywhere in the query, it causes the rows to be aggregated. Since you don't have GROUP BY, everything is aggregated into a single row in the result set (just as you quoted: in absence of group by, entire data is taken as a single group). In this result set, EmpTran and EmpName are taken from arbitrary rows in the table (they might not even be from the same row).
HAVING then filters this result set. If the selected value of EmpTran doesn't match MAX(EmpTran), the row is removed from the result set and you get an empty result.
The order of processing is:
Use WHERE to select the rows to put in the result set.
Aggregate the result set if necessary.
Use HAVING to filter the aggregated result set.
I don't think there's a way to do this without a subquery in MySQL 5.x. In MySQL 8.x you can do it with a window function (I'm not familiar with these, so I'm not going to show it in my answer).
As Barmar has already explained, your second query won't work because finding the max of a column requires a formal separate subquery. This was the case for MySQL versions earlier than 8+. Starting with MySQL 8+, which introduced window functions, we could try something like this:
SELECT *
FROM
(
SELECT *, MAX(EmpTran) OVER () max_val
FROM test
) t
WHERE EmpTran = max_val;
Demo
The demo is in SQL Server, because Rextester does not yet support MySQL 8+. But, it should run on any database which implements the ANSI standard for window functions.
I'm working through an SQL injection tutorial. I don't understand one aspect of an SQL statement which is used determine where the different columns in the table will be displayed on the web page and then used to execute statements. A previous SQL injection statement has been used to determine the number of columns in the table, which is 6. The SQL statement is
SELECT * FROM TableName Where id=12 union all select 1,2,3,4,5,6
I've researched the SELECT and UNION ALL statements and haven't been able to work out what is actually going on. My thinking is that the numbers in the 2nd select statement respresent the column numbers.
The second statement used to get the values from the table is:
SELECT * FROM TableName Where id=12 union all select 1,2,3,4,user(),6
What does the select 1,2,3,4,5,6 and select 1,2,3,4, user(),6 component of the SQL injection query actually do?
They are not column numbers but values. Assuming you can somehow inject the statement you now need something to do with it. The first example counts the columns. theUNION will fail when there are not enough columns. By adding more columns to the UNION eventually the statement will execute. Now you know how many columns there are.
The second one is injecting the user into the return result set. Assuming the result set gets displayed on the screen for some reason, you now have a user name (or service account name) with which to execute more statements on your database, escalate privileges or make service calls.
It's doing something like that. Without knowing more it's hard to know what exactly.
I have been doubting the way I am currently converting ini commands to from SQL where I only want to update something if the account/row exists(or if a field matches/doesn't match a specific value).
The way I am currently doing it is:
Calling a SELECT query
Grabbing the results from the SELECT query
Calling an UPDATE query based on the conditions from the SELECT query.
Is there a way I can do this without making two callbacks?(The plugin that I am working off of utilizes callbacks since it's threaded).
You can combine them together:
UPDATE x WHERE
y = (
SELECT z WHERE n
)
Just replace the variables for your own SQL!
Hope this helps:)
I have a big Access database I am working on, designed by someone else. I am trying to improve it. The way it was set up, first you run Query 1 which makes Table A. Then, run Query 2, which used Table A among others, and made Table B. Then run Query 3, which uses Table B and makes Table C. Then run Query 4, which uses Table C and makes Table D. The final output we use is Table D.
My improvement here was to change Query 1, Query 2, and Query 3 to Select queries, instead of make table queries, and changing the SQL simply by find and replace (for example, in Query 2, replace all the instances of Table A with Query 1 instead). Query 4 is still a make table query and it makes Table D which I export to Excel later.
My question: Can I just run Query 4? That is, will that automatically run Query 3, which will automatically run Query 2, which will automatically run Query 1? Or, do I need to run Query 1 first, then run Query 2, then run Query 3, then run Query 4?
Perhaps beyond that, can I make Query 4 a Select query also? Then, I wouldn't even run Query 4. Instead, I would just export Query 4 itself to Excel and I'm wondering if that would automatically run Query 4, which would run Query 3, and so on.
Just to be clear: I'm not looking for any SQL tips here. I want to keep it just using Access (which is a front-end for SQL, I know) for now.
Note: I realize that one answer here is, "Why don't you try it?" I have and it seems to work as I think it should. The problem is, I have already run all the queries many times before, so I don't know if that makes everything work for now but maybe it won't work the same way later if I haven't already run the previous queries.
Thanks for any help
As Olivier already mentioned, yes selecting from SELECT queries works as you expect. You can actually test this by creating this function in a module
Function LogQueryCall(ByVal query As String)
Debug.Print query & " " & Now
End Function
an then calling it from your queries e.g.
Query1
SELECT *, LogQueryCall("Query1") FROM Table1 ;
Query2
SELECT *, LogQueryCall("Query2")
FROM Query1
INNER JOIN Table2
ON Query1.Field = table2.Field
Query3
SELECT *, LogQueryCall("Query3") FROM Query2
Then you'll see the results in the Immediate Window
Query1 01/03/2012 5:54:46 PM
Query2 01/03/2012 5:54:46 PM
Query3 01/03/2012 5:54:46 PM
Note the function will only get called once per query not once per row
Another option is to create 1 big query. This would use the contents of each query in the from clause like so.
SELECT *
FROM
(Select * FROM Table1) A
INNER JOIN Table2
ON a.Field = table2.Field
The short answer is, "Yes, you can nest SELECT queries and just export to Excel by just exporting the outer-most query result".
Queries can be used exactly like tables in many situations. The nested queries will execute their own SELECT statement and pass their result to the surrounding query as if it was a table.
If query2 calls query1 and
query3 calls query2 and
qyery4 calls query3
... then the queries will automatically be executed in the order shown below by passing their result to the next query when you execute query4:
table -> query1 -> query2 -> query3 -> query4
I assume that your queries look something like this:
query1: SELECT * FROM table;
query2: SELECT * FROM query1;
query3: SELECT * FROM query2;
query4: SELECT * FROM query3;