I use this code to order some virtuemart products how i want and the result is like the photo
SELECT m8jnx_virtuemart_product_categories.* ,m8jnx_virtuemart_products_el_gr.product_s_desc
FROM m8jnx_virtuemart_product_categories
INNER JOIN m8jnx_virtuemart_products_el_gr ON m8jnx_virtuemart_product_categories.virtuemart_product_id = m8jnx_virtuemart_products_el_gr.virtuemart_product_id
where `virtuemart_category_id` =272
GROUP BY `m8jnx_virtuemart_product_categories`.`virtuemart_product_id`
ORDER BY `m8jnx_virtuemart_products_el_gr`.`product_s_desc` desc
I want to set to the ordering an increasing value for this order I have do is this possible?
edit - I mean set in the first row ordering =1 in the second ordering=2, in third 3 etc -
Edit i found this sql queries in an other post but i don't know how to make work in my case that i use allready an inner join
UPDATE Test
SET Number = rowNumber
FROM Test
INNER JOIN
(SELECT ID, row_number() OVER (ORDER BY ID DESC) as rowNumber
FROM Test) drRowNumbers ON drRowNumbers.ID = Test.ID
edit The question is not duplicate I want to update the 'ordering' column that already exist not display only a column with an incresing value
Related
How can you please help me I have a table in the database with a column called "recioient_id"
The value of its rows is repeated more than once How do I prevent repeating rows while keeping the order descending and displaying the last row
I have tried so much without a find
I did this query and was good at preventing repetition and did not display the last row
Query which was:
SELECT *
FROM `messages`
WHERE `sender_id` = 1
GROUP BY `recioient_id` DESC
HAVING COUNT(*) >= 1
Here is a sample table
I hope to be successful in translation and hope to help
If you want one row per recioient_id, use filtering. I would recommend a correlated subquery:
SELECT m.*
FROM messages m
WHERE m.sender_id = 1 AND
m.messages_id = (SELECT MAX(m2.messages_id)
FROM messages m2
WHERE m2.sender_id = m.sender_id AND
m2.recioient_id = m.recioient_id
)
ORDER BY m.recioient_id;
We had a bug in our code that cached the wrong values to the last_order_id (expected_previous_order) column. The query I wrote properly finds the correct last order id but is too slow for our data set.
I want to select this data into another table but I cannot because the query will take too long
I have setup a simple example here with a smaller data set. Original table has about 170k rows.
SQL Fiddle of my Example
In the example:
original_artwork_id is how these rows are grouped.
order_id is the current rows order id
actual_previous_order is the corrected last order id
expected_previous_order is the currently stored last order id. This is the wrong value as it does not actually reference the last order id
EXPLAIN Results
EDIT
Every time a reorder is placed a new entry is placed into the order_artwork table with a reference to the original_artwork_id and last_order_id.
The reference in the current data set to the last_order_id is wrong.
I need to update all records to properly indicate the last order id.
I am doing this by trying to find each artwork and joining it with the previous entry of the same original_artwork_id. Then I can pull the order_id from the last entry to update the current entries last_order_id
Join the current row with the previous row created before the current row with the same original_artwork_id or the current row original_artwork_id = the previous rows id
Not sure if this will be faster than your current query. But anyway.
SQL DEMO
First you need add a new field
`pos` int DEFAULT 0,
And update your base case so can do the JOIN.
update `order_artwork` o
SET `original_artwork_id` = `id`
WHERE `original_artwork_id` IS NULL;
You could use COALESCE(original_artwork_id, id) but cant use index on that case.
Then assign a row_number to each order based in original_artwork_id and date
update `order_artwork` o
left join (
SELECT o.id,
#rn := if(#order_id = `original_artwork_id`,
#rn + 1,
if(#order_id := `original_artwork_id`, 1, 1)
) as rn
FROM `order_artwork` o
CROSS JOIN (SELECT #id := 0, #order_id := 0, #rn := 0) as var
ORDER BY `original_artwork_id`,
`created`
) b on
o.id = b.id
set
o.pos = b.rn;
Finally update the last order.
UPDATE `order_artwork` o
JOIN (
SELECT o1.original_artwork_id,
o2.order_id,
o1.order_id as last_order_id
FROM `order_artwork` o1
LEFT JOIN `order_artwork` o2
ON o1.pos = o2.pos - 1
AND o1.original_artwork_id = o2.`original_artwork_id`
WHERE o2.pos IS NOT NULL
) as b
ON o.original_artwork_id = b.original_artwork_id
AND o.order_id = b.order_id
SET o.last_order_id = b.last_order_id;
I found that the created time column was not reliable. So I decided to just find the last highest order id with the same original_artwork_id.
Create a table that has the corrected values
CREATE TABLE order_artwork_two AS
select
d1.id,
d1.order_id,
max(d2.order_id) last_order_id,
d1.original_artwork_id
from order_artwork d1
left join order_artwork d2
ON d1.original_artwork_id = d2.original_artwork_id
and d1.order_id > d2.order_id
group by d1.original_artwork_id, d1.order_id;
Add an index to the new table. Otherwise the update would be way too slow
alter table order_artwork_two add primary KEY(id);
Update our original table.
update order_artwork d1
left join order_artwork_two d2 on d2.id = d1.id
set d1.last_order_id = d2.last_order_id;
i am running this query, witch consists of 3 recursive selects.
select idigorUserFields
from ( select *
from ( select *
from igorUserFields f
where f.idigorUsers = 1
order by f.idigorUserFields desc) tbl
group by tbl.idigorUserFieldTemplates ) tbl2
where value="qf" and idigorUserFields = 28
what I am trying to do is simple:
get all fields, order by insert date ( i am using primary key for that )
get the last inserted value for a field (idigorFieldTemplates)
compare the last inserted field with the one i am about to insert, to save some space on the database
some relevant info:
idigorUserFieldTemplates is the primary key for a "html user fields table"
the last inserted value is the one displayed on the program
also, I have a sqlfiddle! with some data to test.
my question is: can I make this query better? and what do I need to use to do that.
Alright so it looks like you want to get the last inserted value for a given user and value and then compare that with the data you are about to insert. Here's how I would tackle that
SELECT a.idigorUserFieldTemplates
FROM igorUserFields AS a
INNER JOIN (SELECT MAX(idigorUserFields) as max_id FROM igorUserFields WHERE idigorUsers = 1 AND value="qf") AS b
ON a.idigorUserFields= b.max_id
The subquery is giving me the largest idigorUserFields for which idigorUsers = 1 AND value="qf." For this to work, you have to assume that that the primary key (idigorUserFields) is incrementing with date. You indicated in your post that it does, so hopefully this assumption is okay.
Once we've got that last updated record, we then join back with igorUserFields to get the corresponding value of idigorUserFieldTemplates
edit:
I want to get the last inserted for a given user and field. then compare it with what i am about to insert.
I am sorry if I was not clear enough about what I wanted.
I modified the above sql and i got what i wanted:
SELECT a.* FROM igorUserFields
AS a INNER JOIN
(SELECT MAX(idigorUserFields) as max_id
FROM igorUserFields WHERE idigorUsers = 1 and idigorUserFieldTemplates =6) AS b
ON a.idigorUserFields= b.max_id
AND value="qf"
thanks! now I got a more efficient solution :)
if all you want is the largest id by specific data just specify that order it and limit it like so
SELECT idigorUserFields
FROM igorUserFields
WHERE idigorUsers = 1 AND value="qf"
ORDER BY idigorUserFields DESC
LIMIT 1
DEMO
if you are trying to get the last inserted value then you can get the largest idigorUserFields assuming its auto incremented.
SELECT MAX(idigorUserFields) FROM idigorUserFields
and then if you want a specific column from that field you could use it as a subquery
SELECT f.idigorUserFieldTemplates
FROM idigorUserFields f
WHERE f.idigorUserFields =
( SELECT MAX(idigorUserFields)
FROM idigorUserFields
WHERE idigorUsers = 1 AND value="qf"
)
Here is my mysql query
SELECT network_mst.NetworkId,network_mst.NetworkName,network_mst.InternationalDBStatus,network_mst.Of fshoreDBStatus,network_mst.InterDBStatus,network_mst.IntraDBStatus
FROM network_mst INNER JOIN carrier_network ON network_mst.NetworkId=carrier_network.NetworkId
INNER JOIN user_network ON network_mst.NetworkId=user_network.NetworkId WHERE UserId=1
AND carrier_network.CarrierId IN (1) GROUP BY network_mst.NetworkId ORDER BY network_mst.NetworkName;
The resulting data is
Sorry for the small image.It has a field called NetworkId.
So my question is how to find the row index of a row with NetworkId 2.Here the answer is row index of row with NetworkId 2 is 3.
But how to find this using query
Try this:
SET #rownum = 0;
Select *
FROM
(
Select *, (#rownum := #rownum+1) as rank
FROM
(
-- Your current query here
) t
) sub WHERE rank = 3
This query should return the row with the netword_id = 2 since it is the third row. But you have to watch out the ORDER BY clause in the inner most query since it is what controls the order of the ranking.
OK, I've voted to delete my earlier question due to stupidity on my part...
I have the following code:
SELECT qnum, id, name, total_staff AS StaffCount, COUNT( q61g ) AS TotalResp,
(COUNT( q61g ) / total_staff * 100) AS Perc
FROM tdemog_pfp
LEFT JOIN tresults_pfp ON tdemog_pfp.id = tresults_pfp.q61g
WHERE qnum = 'q61g' AND q60p = '1'
GROUP BY name
ORDER BY name
Now, the first part of this query brings back rows from the tdemog table, for example it will bring back 5 rows of data each row has an id from 1 to 5. What I need the query to do is then bring back data from the tresults table WHERE q60p = 1 for each of the 5 rows brought back in the first part - like a normal `LEFT JOIN'.
Make sense?
H.
Try moving part of your WHERE clause into your JOIN condition:
SELECT ...
FROM tdemog_pfp
LEFT JOIN tresults_pfp ON tdemog_pfp.id = tresults_pfp.q61g AND q60p = '1'
WHERE qnum = 'q61g'
GROUP BY name
ORDER BY name
If you have a field from your second table in your WHERE clause, it will restrict the entire record... but if you put it into your JOIN condition, the record from the first table should still be returned even when the record in the second table doesn't meet the additional criteria...
I'm not sure which column belongs to which table... but move whatever columns are in your second table into your JOIN.