logo
down
shadow

Add multiple items for one orderid in MySQL database


Add multiple items for one orderid in MySQL database

By : M SAAD
Date : November 17 2020, 01:00 AM
it fixes the issue You should add the items in a separate table, orderitems (or orderlines). The order table contains the main information about the order, and each item contains a product and it has an orderid that refers to the id of the main records. That orderid in orderitem is called a foreign key, because it refers to the primary key of another table.
code :
orders
orderid   customer
(auto inc) 
1         John Doe
2         Jane Doe
3         Jim Doe

orderitems
orderitemid orderid   productdesc   quantity 
(auto inc)  (FK)
1           1         test          1          
2           1         value         2
3           1         demo          3
4           2         test          2
5           3         demo          1
6           3         value         1
select
  o.orderid,
  o.customer
  oi.orderitemid,
  oi.productdesc,
  oi.quantity
from
  orders o
  inner join orderitems oi on oi.orderid = o.orderid
where
  o.orderid = 1
select
  oi.orderitemid,
  oi.orderid,
  oi.productdesc,
  oi.quantity
from
  orderitems oi
where
  oi.orderid = 1


Share : facebook icon twitter icon
php/mySQL How do I get all the tags from the database for multiple items?

php/mySQL How do I get all the tags from the database for multiple items?


By : user2816493
Date : March 29 2020, 07:55 AM
wish help you to fix your issue you can use the GROUP_CONCAT() function, which will return the tags comma delimited:
code :
SELECT c.*, GROUP_CONCAT(t.tag) AS Tags
FROM content AS c 
  LEFT JOIN contenttags AS ct ON c.cid = ct.cid 
  LEFT JOIN tags AS t ON ct.tid = t.tid 
GROUP BY c.cid 
LIMIT 5
SQL Server 2005 query, update qtys to 0 where orderid <> min(orderid)

SQL Server 2005 query, update qtys to 0 where orderid <> min(orderid)


By : Fireflyspunch
Date : March 29 2020, 07:55 AM
I hope this helps you . I have a table , You can try something like this:
code :
;WITH CTE AS
(
    SELECT 
        OrderID, Qty, ShopID,
        RowNum = ROW_NUMBER() OVER (PARTITION BY OrderID ORDER BY ShopID DESC)
    FROM
        dbo.YourOrderTableHere
)
SELECT
    OrderID,
    OrderedQty = CASE RowNum    
                    WHEN 1 THEN Qty ELSE 0 
                 END,
    ShopID
FROM CTE    
Select orderId if it has both items bought

Select orderId if it has both items bought


By : DWDG
Date : March 29 2020, 07:55 AM
it should still fix some issue I have a quite common case of two tables: 'Orders' placed and 'ItemsBought' for each order: , Try something like this:
code :
SELECT orderId 
FROM ItemsBought
WHERE itemId IN (1,2)
GROUP BY orderId
HAVING COUNT(orderId) = 2;
Creating columns based off of OrderID Items

Creating columns based off of OrderID Items


By : user2297087
Date : March 29 2020, 07:55 AM
With these it helps I am working with a basic table, and am curious if there is any possibility to have everything for my id to show on the same row? Below I have shown an example of my current query and the output, along with how I'd like to see the output be shown. , You seem to need conditional aggregation :
code :
SELECT
    OrderID,
    MAX(CASE WHEN item = 'apple' THEN weight END) Apple,
    MAX(CASE WHEN item = 'banana' THEN weight END) Banana,
    MAX(CASE WHEN item = 'orange' THEN weight END) Orange,
    MAX(CASE WHEN item = 'grape' THEN weight END) Grape
    Location
FROM samples
GROUP BY OrderID, Location
How do you add OrderID to this? I cant get it to display without getting a error on all OrderID sections

How do you add OrderID to this? I cant get it to display without getting a error on all OrderID sections


By : Jean-Luc Bellemare
Date : March 29 2020, 07:55 AM
help you fix your problem It really helps for you to put some effort into a script that demonstrates your problem. That way no one needs to guess about how you defined things and the actual values you use.
Here is one approach based on my guess. Note "one" - there are techniques to achieve the same result. The cte calculates the total cost for each order just like your code attempted. Since you suggested top, I used that as well to demonstrate its usage. Grabbing the top first row based on descending OrderCost will get you the order with the highest total cost. Just join the cte to Orders and Customers to include the columns you desire.
code :
with cte as (select OrderID, sum(ItemPrice * Quantity) as OrderCost 
  from @OrderItems 
  group by OrderID)
select top 1 Ord.OrderID, Ord.OrderDate, cust.EmailAddress, cte.OrderCost
from cte inner join @Orders as Ord 
  on cte.OrderID = Ord.OrderID
inner join @Customers as Cust 
  on Ord.CustomerID = Cust.CustomerID
order by cte.OrderCost desc; 
Related Posts Related Posts :
  • MySQL join optimization - bypassing filesort
  • How to Select record with the maximum value in a GROUP BY?
  • MySQL Innodb Full text Proximity Search Gives Horrible Performance
  • MySQL best approach for db normalising, relationships and foreign keys
  • How to update one table based on the count value of another table while matching ids?
  • MAMP Pro 5.1.1-terminates MySQL when quitting
  • SQL - Get date of most votes cast
  • "Fuzzier" search in mysql
  • Tracing mysqld Using SystemTap
  • SELECT + INSERT + Query Cache = MySQL lock up
  • SQL vs. NoSQL for medium complexity search systems
  • SQL Error when creating table
  • Is it possible to write a query to compare rows to other rows in same table?
  • Display all duplicate records based multiple columns
  • summing time column in mysql
  • InnoDB: Cannot open table
  • Why is IN() considered O(logN) operation?
  • Can a deadlock occur on commit?
  • Is there a fast way to do a '%phrase%' search in any DB?
  • Is using an IN over a huge data set a good idea?
  • How can I know if a table is already arranged in ascending order in MySQL?
  • MySQL - Designing database for a simple game
  • Why am I getting "Host '192.168.1.220' is not allowed to connect to this MySQL server"?
  • what type of password hash? possible mysql hash?
  • MySQL - Using a date range vs functions
  • Using IS NULL and COALESCE in OrderBy Doctrine Querybuilder
  • How to merge MySQL columns into one column if there are empty values in the table?
  • Cardinality Confusion
  • Renaming column multiple ids in SELECT
  • Group all from and to messages per user
  • How to handled the null values in WHERE IN condition in mysql
  • Is it okay to update primary key field to shift a record to the top?
  • Stop query through pdo
  • How to check whether particular value in field in MySQL exists
  • Execute a statement for every table in a database
  • insertion 74G data into mysql table cost more then 2 days, how to improve insert performance
  • Where can I see the differents "Hibernate Console Configurations" in Eclipse
  • MySql | relational database vs non relational database in terms of Performance
  • Exporting mysql database using mysqldump including procedures
  • Trigger MySQL not working
  • How do I update values for individual rows quickly?
  • MySQL join query with multiple where condition
  • MySQL stored procedure is not working
  • MySQL - Order By exact match at the end
  • Select max date or null date from same table MySQL
  • Best solution workaround for needing a mysql event on insert?
  • MySQL select several columns of several tables at the same time without using JOIN
  • Is it more efficient to have a table with one of its columns mostly empty or make a new table and refer to it?
  • mysql data truncated with ñ character
  • MySQL Distinct Active Users for the last month query
  • Get all rows that match another attribute in the same table
  • MySQL statement to get min value with max ID
  • DATEDIFF Current/Date for Last Record
  • What is the logic or procedure for combining multiple DB query's results into one elegant table?
  • Modify TIMESTAMP MySQL for use as Primary Key
  • Django - Efficiently bulk create inherited models
  • MySQL Dual table Join Dual table
  • Ruby on Rails - database where sorted by id
  • MySQL 5.6 : Access denied for user 'root' @ 'localhost' when password contains backslash
  • sql query case dateadd
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org