I want to store the results of query
SELECT column_name from table_name limit 2
into variables, says, #w1 , #w2
What are the correct syntax?
I tried
"Set #w1, #w2 = (SELECT column_name from table_name limit ); "
and similar ones but it hasn't work.
Thank.
You can try the following:
SELECT * FROM
(SELECT #a := (SELECT column_name FROM table_name ORDER BY value LIMIT 1) as a) a,
(SELECT #b := (SELECT column_name FROM table_name ORDER BY value LIMIT 1 OFFSET 1) as b) b
Please note that limit won't make much sense if you do not use ORDER BY.
Related
How do you run an "order by" and then "replace" string, but keep the order? Context - I need the string "column_name" to be in the first line, hence using "zzz_column_name" to force it in the order by. And then I need to use replace to change it from "zzz_column_naem" to "column_name".
SELECT replace(column_name, 'zzz_', '')
FROM (
SELECT *
FROM (
SELECT 'zzz_column_name' AS column_name
UNION
SELECT column_name
FROM table
) s
ORDER BY column_name DESC
) a
After the replace in the first line, I'd lose the order achieved by order by.
Just order by the unmodified column. You don't even need a subquery:
SELECT replace(column_name, 'string', '') AS column_name_replaced
FROM (
SELECT 'zzz_column_name' AS column_name
UNION ALL
SELECT column_name FROM table
) s
ORDER BY column_name DESC
Note: UNION ALL is more efficient than UNION - use it unless you have good reasons not to
I am wondering if you are actually trying to put first the row that has the fixed value; in that case, we can simplify the whole thing with just a conditional sort. Assuming a table like mytable(col), we can add a header row with value 'my_header' like so:
SELECT 'my_header' AS col, 1 AS is_header
UNION ALL
SELECT col, 0 FROM mytable
ORDER BY is_header DESC, col
This puts the header row first, followed by all values in order.
If you mind the additional column, we can remove it with a subquery:
SELECT col
FROM (
SELECT 'my_header' AS col, 1 AS is_header
UNION ALL
SELECT col, 0 FROM mytable
) t
ORDER BY is_header DESC, col
You don't need the outer SELECT and you can sort by the original column name
SELECT replace(column_name, 'string', '')
FROM (
SELECT 'zzz_column_name' AS column_name
UNION
SELECT column_name
FROM table
) s
ORDER BY column_name DESC
I use this code in MySQL to order by 'anotherColumn' and then get the row number of 'myColumn' and then I perform a calculation and set 'myColumn' to the result:
SET #c = (SELECT COUNT(*) FROM myTable); SET #rownum = 0; UPDATE myTable SET myColumn = #c * (#rownum:= 1 + #rownum) ORDER BY anotherColumn DESC LIMIT 100000;
I'm trying to achieve the same thing in Postgresql but am getting a lot of errors. I have:
SET c = (SELECT COUNT(*) FROM myTable); SET rownum = 0; UPDATE myTable SET myColumn = c * (rownum:= 1 + rownum) ORDER BY anotherColumn DESC LIMIT 100000;
.. but it gives me an error at the first parenthesis. If I remove those parenthesis like this:
SET c = SELECT COUNT(*) FROM myTable; SET rownum = 0; UPDATE myTable SET myColumn = c * (rownum:= 1 + rownum) ORDER BY anotherColumn DESC LIMIT 100000;
.. then it gives me an error at the SELECT. If I just set c to equal 0, I get an error way down at the ORDER. Does anyone know how to convert my code from MySQL to PostgreSQL?
This "pattern" in MySQL is typically used to work around the absence of window function.
You don't need variables in Postgres to achieve something like that:
update my_table
set my_column = t.cnt + t.rn
from (
select pk_column,
(select count(*) from my_table) as cnt,
row_number() over (order by another_column) as rn
from my_table
limit 100000
) t
where t.pk_column = my_table.pk_column;
Where pk_column is the primary key column of your table. If you have more than one PK column, you need to use all of them.
I tried two methods but failed in mysql.
/*see top 50% students, but this sql can't work*/
select * from student_table order by chinese_score desc limit count(*) * 0.5 ;
/*also can't work*/
set #num= floor((select count(*) from test.student_score)*0.5);
select * from student_table order by chinese_score desc limit #num ;
How to solve in mysql?
In Mysql this can be done in a single query using user defined variables.
You can store a value in a user-defined variable in one statement and
refer to it later in another statement. This enables you to pass
values from one statement to another.
SELECT * FROM (
SELECT student_table.*, #counter := #counter +1 AS counter
FROM (SELECT #counter:=0) AS initvar, student_table
ORDER BY student_table.chinese_score DESC
) AS result
WHERE counter < (#counter/2) ORDER BY chinese_score DESC;
I want to select the last 3 rows of an sql table. I know I should use SELECT * FROM table ORDER BY DESC LIMIT 3, but the problem with this code is that it selects the rows from the end. For example, it selects 30, then 29, then 28. But, I need them in this format: 28, 29, 30. Any suggestion?
Try this:
SELECT * FROM (
SELECT * FROM reset ORDER BY id DESC LIMIT 3
) as r ORDER BY id
I hope this help your problem
select * from
(
select * from reset
order by id DESC LIMIT 3
) t
order by id ASC
Try something like this:-
SELECT * FROM reset
WHERE username = '$table' ORDER BY id ASC LIMIT (FOUND_ROWS() - 3), 3
How about something like:
select * from (select * from table order by x desc limit 3) order by x;
try
Select * from (SELECT * FROM Table_name ORDER BY Column_name DESC limit 0,3) as alias ORDER BY Column_name ASC;
try this manual one !
easy and Simple !!
Select * From tableName where
PKCol=(select count(*) from tableName )
OR
PKCol=(select count(*) from tableName )-1
OR
PKCol=(select count(*) from tableName )-2
order by PKCol desc;
It will help you out to give latest 3 rows data, if you wanna take first 3 rows then ASC instead of DESC.
select distinct column_name from Table order by column_name desc limit 3;
I would like to insert max 3 results of a column into 3 different columns.
SELECT
t.name,
m.top_marks
FROM
table_name t,
(SELECT
marks
FROM table_name
WHERE rownum <=3
ORDER BY marks DESC) m
GROUP BY column_name DESC;
This can help me to get top 3 marks right? but what if i want to store top 3 results in 3 new columns? Marks1, marks2, marks3?
I can't say I really understand what you are going for... But it sounds like you might want the LIMIT operator. See this:
SQL - Select first 10 rows only?
So maybe something like this?
SELECT
t.name,
m.top_marks
FROM
table_name t,
(SELECT
marks
FROM table_name
ORDER BY marks DESC
LIMIT 3) m
GROUP BY column_name DESC;
You can do this with the group_concat()/substring_index() method:
SELECT t.name,
substring_index(m.marks, ',', 1) as Mark1,
substring_index(substring_index(m.marks, ',', 2), ',', -1) as Mark2,
substring_index(substring_index(m.marks, ',', 3), ',', -1) as Mark3
m.top_marks
FROM table_name t cross join
(SELECT group_concat(marks order by marks desc) as marks
FROM table_name
) m
GROUP BY column_name DESC;
If it is sufficient to have them in one column with commas separating the values:
SELECT t.name, substring_index(m.marks, ',', 3) as Marks3
FROM table_name t cross join
(SELECT group_concat(marks order by marks desc) as marks
FROM table_name
) m
GROUP BY column_name DESC;