I'm using the following query:
select id,link,size from files where status='-1' ORDER BY id DESC LIMIT 10
then I want to change the 'status' column values of the selected rows to -2, is it possible to combine the 'select' and 'update' functions in the same query so that I don't have to query the database again?
You do not need to combine the select with an update:
try:
update files set status='-2' where status='-1' order by id desc limit 10;
The fields names are irrelevant here.
Related
I have one such sql:
select name from A where id in (23,24,22,23)
When I run it in Navicat, the result only have one result of 23.
My question is, how to keep the number and order of the query results remains the same as (23,24,22,23).
If you want to maintain the order of the result then use order by clause like
select name from A
where id in (23,24,22)
order by id;
Again, assuming that id is a primary key column in your table A then there will be only one row with id = 23. How do you expect the same row to get repeated automatically unless you make it explicit by using a UNION ALL
If you really really want to fetch the records like this, you can use field function to get 23,24,22 and order by this sort:
select name from A where id in (23,24,22) order by field(id, '23,24,22')
then use union all get another 23:
(select name from A where id in (23,24,22) order by field(id, '23,24,22'))
union all
select name from A where id = 23
I am trying to concatenate 2 columns, then count the number of rows i.e. the total number of times the merged column string exists, but I don't know if it is possible. e.g:
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(merged_columns)
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
Note: the colon I've inserted as a part of the string, so my result is something like 12:3. The 'count' then should tell me the number of rows that exist where column_1 =12 and column_2 = 3.
Obviously, it tells me 'merged_columns' isn't a column as it's just an alias for my CONCAT. But is this possible and if so, how?
Old question I know, but the following should work without a temp table (unless I am missing something):
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(CONCAT(column_1,':',column_2 ))
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
You can try creating a temp table from your concatenation select and then query that:
SELECT CONCAT(column_1,':',column_2 ) AS mergedColumns
INTO #temp
FROM table
SELECT COUNT(1) AS NumberOfRows,
mergedColumns
FROM #temp
GROUP BY mergedColumns
Hope this answer is what your are looking for.
Try this
SELECT
CONCAT(column_1,column_2 ) as merged_columns,
COUNT(*)
FROM
table
GROUP BY merged_columns
ORDER BY merged_columns DESC
I'm trying to insert using a select statement. However, I need to order the sub-select results using a ranking equation. If I create an alias, it throws off the column count. Can I somehow order my results using an equation?
INSERT INTO draft
( fk_contrib_id , end_time )
SELECT pk_contrib_id, UNIX_TIMESTAMP(), (X+Y+Z) AS ranking
FROM contrib
ORDER BY ranking DESC
LIMIT 1
I need the 'ranking' column for sorting, but if I do, the column count is off for the insert. Do I have to use two queries for this?
You could simply change your query to directly use the expression in the ORDER BY clause, like so:
INSERT INTO draft
( fk_contrib_id , end_time )
SELECT pk_contrib_id, UNIX_TIMESTAMP()
FROM contrib
ORDER BY (X+Y+Z) DESC
LIMIT 1
Remove the expression from the SELECT list. And use the expression in the ORDER BY clause.
ORDER BY X+Y+Z
It's perfectly valid to ORDER BY expressions that are not in the SELECT list.
I am fetching rows from my DB using this request:
SELECT * FROM {$db_sales} WHERE date = '{$date}' ORDER BY 'amount' DESC
So, obviously, i expected the returned values to be sorted in descending order by the amount column in my DB, but it doesn't? it still fetches them, but just doesn't sort them?
Any ideas here? is my SQL statement wrong?
remove single quote around amount like this and try:
SELECT * FROM {$db_sales} WHERE date = '{$date}' ORDER BY amount DESC
Use below query
SELECT * FROM {$db_sales} WHERE date = '{$date}' ORDER BY amount DESC
ORDER BY clause uses column name.
Column name should not give in quotes.
there fore the query becomes as follows
SELECT * FROM {$db_sales} WHERE date = '{$date}' ORDER BY amount DESC
I am new to MYSQL, and unable to resolve or even with so many answers on this forum, unable to identiy the error in this statement. I am using MYSQL database.
I have 2 tables: Ratemaster and rates, in which a customer can have 1 product with different rates.
Because of this, there is a duplication of customer and product fields, only the rate field changes.
Now Table Ratemaster has all the fields : id, Customer code, Product, Rate, user
whereas Table Rates has only: id, cust code, Rate, user.
- user field is for checking session_user.
Now Table Ratemaster has 3 records with all field values being same except Rate field empty.
Table Rates has different rates.
I want to have all rates to be updated in Ratemaster from Rates table. I am unable to do this with UPDATE and LIMIT mysql command, it is giving error as:
Incorrect usage of UPDATE and LIMIT
UPDATE Ratemaster, Rates
SET Ratemaster.Rate=Rates.Rate
WHERE Ratemaster.user=Rates.user
LIMIT 1
Usually you can use LIMIT and ORDER in your UPDATE statements, but in your case not, as written in the MySQL Documentation 12.2.10. UPDATE Syntax:
For the multiple-table syntax, UPDATE updates rows in each table named
in table_references that satisfy the conditions. In this case, ORDER
BY and LIMIT cannot be used.
Try the following:
UPDATE Ratemaster
SET Ratemaster.Rate =
(
SELECT Rates.Rate
FROM Rates
WHERE Ratemaster.user = Rates.user
ORDER BY Rates.id
LIMIT 1
)
Salam
You can use this method and work properly !
UPDATE Ratemaster, Rates
SET Ratemaster.Rate=Rates.Rate
WHERE Ratemaster.user=Rates.user
ORDER BY Rates.id
LIMIT 1
Work It 100%
UPDATE table SET Sing='p' ORDER BY sr_no LIMIT 10;
Read article about
How to use ORDER BY and LIMIT on multi-table updates in MySQL
For the multiple-table syntax, UPDATE updates rows in each table named
in table_references that satisfy the conditions. In this case, ORDER
BY and LIMIT cannot be used.
The problem is that LIMIT is only to be used with SELECT statements, as it limits the number of rows returned by the query.
From: http://dev.mysql.com/doc/refman/5.5/en/select.html
The LIMIT clause can be used to constrain the number of rows returned by
the SELECT statement. LIMIT takes one or two numeric arguments, which
must both be nonnegative integer constants, with these exceptions:
Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
With two arguments, the first argument specifies the offset of the
first row to return, and the second specifies the maximum number of
rows to return. The offset of the initial row is 0 (not 1):
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
To retrieve all rows from a certain offset up to the end of the result
set, you can use some large number for the second parameter. This
statement retrieves all rows from the 96th row to the last:
SELECT * FROM tbl LIMIT 95,18446744073709551615;
With one argument, the value specifies the number of rows to return
from the beginning of the result set:
SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows
In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.
For prepared statements, you can use placeholders. The following
statements will return one row from the tbl table:
SET #a=1; PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?'; EXECUTE STMT
USING #a;
The following statements will return the second to sixth row from the
tbl table:
SET #skip=1; SET #numrows=5; PREPARE STMT FROM 'SELECT * FROM tbl
LIMIT ?, ?'; EXECUTE STMT USING #skip, #numrows;
For compatibility with PostgreSQL, MySQL also supports the LIMIT
row_count OFFSET offset syntax.
If LIMIT occurs within a subquery and also is applied in the outer
query, the outermost LIMIT takes precedence. For example, the
following statement produces two rows, not one:
(SELECT ... LIMIT 1) LIMIT 2;