TABLE cartitems
CartItemId|CartId|sku_id|prod_id|quantity
1             1         2          1           1
2             1         2          1           1
I want to merger the rows having same sku_id and update the quantity and delete one duplicate row like this
CartItemId|CartId|sku_id|prod_id|quantity
1             1         2          1           2
if delete doesnt happens its okay. i l manage it later.
it's simple Aggregate function with an ORDER BY Keyword to
use.
try,
select
min(CartItemId),
min(CartId),
sku_id,
Sum(quantity) from cartitems group by sku_id
Related
i have a one question for increment +1 in every row
i have a sql table in that table have order_status column when new record created order_status will be store +1 then older record in that case if i have order status like 0 1 2 3
but when user send me order status 2 for 5th record so in this case record no 5 will be store as a record status 2 but i need to change status aftr 2 like
2 will be 3, 3 will be 4, 4 will be 5
if its posible so please me sql query
Use an UPDATE query.
UPDATE yourTable
SET order_status = order_status + 1
WHERE order_status >= 2
Here is the table structure. I want to fetch all the id_product which have value 2 and 601(here id_product = 5). If i use OR all the records will be populated which is not necessary.
id_product attribute_id Value
5 2 2
6 2 2
8 2 601
6 2 601
5 3 601
8 3 32
6 3 41
Any help would be appreciated. I don't want to use sub query :-p
You can use a group by query:
select
id_product
from
tablename
where
attribute_id=2 and values in (2,601)
group by
id_product
having
count(*)=2
this will select all products that have (attribute_id=2 and value=2) or (attribute_id=2 and value=601) in two different rows, and then it will count the number of rows returned and select only products that have two rows (one with value 2 and one with value 601).
Another option (it's not too clear from your question) is to use this where clause instead of the one on the query above:
where
(attribute_id=2 and value=2) or
(attribute_id=3 and value=601)
You can use this query in your case:
SELECT * FROM nameTable WHERE Values IN (2,601) and attribute_id = 2
I have a Table table .
Now this has three columns table_id,content_id,content_type
What i want is to SELECT rows based on unique pairs of columns.
Say For example i have rows like this-->
id|content_id|content_type|
1 1 shirt
2 1 trouser
3 4 skirt
4 4 shirt
5 3 trouser
6 5 jeans
7 1 trouser
8 5 jeans
I want a query which selects Rows with id->1,2,3,4,5,6.
Rows with id->7,8 are not to be selected
Therefore it concludes that i dont want to select complete Duplicates of Rows
You can use a self join to pick a minimum row per group
select t.* from
test t
join (
select min(id) id ,content_id,content_type
from test
group by content_id,content_type
) t1
on(t.id = t1.id
and t.content_id = t1.content_id
and t.content_type = t1.content_type)
Demo
or if there are only these 3 columns in your table then you can simply use min()
select min(id) id ,content_id,content_type
from test
group by content_id,content_type
Demo
This is mysql-specific : If you use the GROUP BY function without using aggregate functions, the group by clause will behave as distinct, and pick up the first distinct row.
select id, content_id, content_type from test group by content_id, content_type order by id
Demo
Is there a way I could somehow do the following?
Table ONE
id c_id type
-------------------
1 1 7
2 2 7
3 3 5
4 4 7
Table TWO
id title live
-------------------
1 row1 1
2 row2 0
3 row3 0
4 row4 1
The c_id column links the data from table ONE to table TWO. Example: in table ONE, if the c_id is 2, that row in table ONE will be directly linked to table TWO's row with id 2, which has a title of "row2".
I want to select from table ONE, everything with type 7, but only if their associated data in table TWO has live set to 1.
Here's how I thought I'd do it, but this doesn't seem to work:
SELECT * FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' ORDER BY ONE.id DESC LIMIT 5
I would expect the above to return only rows 1 and 4 from table ONE, because although table ONE has three rows with type "7", only rows 1 and 2's associated row in table TWO have live set to 1.
You were close... try using an implicit join:
SELECT ONE.* FROM ONE, TWO WHERE ONE.type='7' AND TWO.live='1' AND ONE.c_id = TWO.id ORDER BY ONE.id DESC LIMIT 5
select * from one join two on c_id = two.id where type=7 and live = 1
order by one.id desc limit 5
I do not know how to classify this question. Vaguely, its about using calculated value in the WHERE clause of a mysql query performed using a php script.
Here's the scenario -
I've a mysql table with structure like this: table_id[int], item_id[int], item_rating[int]
Now the item_rating column can have either a "1" or a "0" value in it. The table_id column is set to auto_increment and the item_id column can have duplicate values also.
So a typical table will look like below -
table_id item_id item_rating
1 item1 1
2 item5 0
3 item1 1
4 item1 1
5 item5 1
6 item1 0
What i intend to do i for each item_id, i count the number of item_rating = 1 and item_rating = 0 and then take the difference of item_rating values to get the final rating for that item (final_item_rating = item_rating(with value=1) - item_rating(with value=0) ).
Now the issue:
I have a php script that takes values from these tables, and then displays the item details ordered on the "final_item_rating" value - something like:
select * from table_name order by final_item_rating desc
only problem is, since this final_item_rating is not a column in itself, and is actually based on run time value of the query, how do i build a query?
hope i have the question clear :)
This query may help you:
SELECT sum(item_rating) AS SumRatings
FROM table_name
WHERE item_rating=1
GROUP BY item_id
ORDER BY SumRatings;
This query would sum the ratings, and order the result with the highest rating on top:
select item_id
, sum(case when item_rating = 1 then 1 else -1 end) as rating
from YourTable
group by
item_id
order by
sum(case when item_rating = 1 then 1 else -1 end) desc