Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    MYSQL




How To Count The Number Of PRODUCTS Bought In A Cart.


A shopping cart has a table called "cart" where the customer's selections are stored.

The customer buys 1 item of Product C, 2 items of Product H and 3 items of Product T.

I need to be able to count the number of PRODUCTS bought NOT the number of items. i.e. the answer here is 3.

What is the EASIEST way to do this?




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Total Number Of Products
I need ro run an SQL that gives me total number of products in each brand.

Count Of Products Under Category
I have a table of categories with c_id, c_name, parent_id
by which I handle the relation of categories & sub-categories.
Level is restructed upto 3 (may need not to consider)
products table has p_id, p_name, vchCategories (comma separated)
One product can be associated with more than one category, this is comma separated, I use FIND_IN_SET to in queries for this field.
My question:
I want to select all the categories with count of products associated with that category. (Which should include the count of products of sub-categories too)
I am using mySql5. How can I do this?

Count Number Of Columns
I have a table in which all columns except the primary key are type boolean (tinyint(1) in mysql). I want to be able to compare two specific rows, referenced by their primary key, and count the number of columns in which they both have a "1". As a second search, returning the names of these columns would be helpful too.

Count Number Of Columns In A Table
Using MYsql 4.0. I have a table with A LOT of columns. Is there a syntax/query that will give me the number of columns in my table??

Count Number Of Rows Returned?
this is my connect and everything code:

<?php // connect to the mysql database server.
mysql_connect ($dbhost, $dbusername, $dbuserpass);
mysql_select_db($dbname) or die(mysql_error());
$query = "SELECT DISTINCT category FROM $tuttable";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
echo ($row['category']." | ");
}
echo count($row);
?>

Notice "echo count($row);" How do i get this to work properly. It only returns one, because there is only 1 that it is working on. I want to return the count of all the rows that fit that query.

Trying To Count The Number Of Rows In A Result Set After Query
The user fills out this form to sign up to the website, the form checks the database to see if the username has already been taken with the code:

$conn = mysql_connect("localhost:3306", "root", "********")
                                    or die ("Error With Connection");
        echo("connected<br><br>");
        $db_sel = mysql_select_db("game",$conn)
                                    or die ("Error With Database");
        $check = "select * from users where 'username' = '$username'";
        $db_sel = mysql_query($check,$conn)
                or die (mysql_error());

Return A Count Of Number Of Rows Before Desired Row.
Anyone know how? find_in_set doesn't seem to be able to do what I want since I am not searching through an array but am actually running a query.

What I really want is to be able to select all until something = something. I've searched the realm of google but cannot come up with anything useful, and so I am wondering if maybe I don't know the right words to be looking for.

Join Tables To Count Number Of Records?
I've two tables:

responses
==========
response_id
schema_id
timestamp

answers
==========
answer_id
response_id
answer

(that's a cut down version, but will do for this question)

I need to get all the responses where there is at least one answer in the answers table. But I do not want the answer data. It's literally a quick check for an export to say "get me all the responses where there's at least one question answered". I have this: Code:

Get The Cart
I'm hoping there's a simple answer to this seemingly simple question. I'm currently working with this script at the moment: http://www.thewatchmakerproject.com/journa...p-shopping-cart
Everything seems to work pretty well, but I do have a question. In the functions.inc.php file the cart is only selecting from the 'books' table which is fine, but I have more than one table that I need it to select from. Is there anyway to get it to select from any table in my database or just the tables I need

USING MYSQL WITH X-CART
I'M TRYING TO SET UP MYSQL TO WORK WITH X-CART (SHOPPING CART SOFTWARE). THE X-CART SOFTWARE IS ASKING ME FOR MySQL user name, MySQL database name and MySQL password. I already set up the MySql database and I dont know where to get this info.

Keep The Cart Data Persistent Or Not?
I am designing a e-Commerce site and have come up with an idea to share the cart data in a more persistent way then is usually implemented to reduce redundancy in the database. However I would like to filter the idea through sitepoint users to see if you think it is a bad idea, and if so why, or have you had any experience using this method?

Here is the scenario:

You create a customers basket:

customers_basket_id
session_id
customer_basket_date_added
has_been_invoiced
invoiced_date

You create a mapping table

product_id (FK)
customers_basket_id (FK)

You create an invoices table

customers_basket_id (FK)
invoice_id
invoice_date
invoice_last_modified_date
invoice_amount
tax_id (FK)
invoice_paid_in_full
invoice_remaining_balance

You create a mapping table

payment_id (FK)
invoice_id (FK)

In this scenario to generate the invoice the SQL would have to join the Invoice table, the customers_basket table, and the products table.

If the has_been_invoiced flag is NOT set then the Cart would be deleted in, say for instance, 7 days, and no invoice or payments would yet be created; otherwise the cart would be saved permenantly as it is used to generate the final output of the invoice to a Web user.

One of the down-sides of this approach is a somewhat robust query must be used everytime an invoice is generated. However usually an invoice is not generated or changed too often; therefore it could be a reasonable flow.

One advantage is that data does not need to be managed or duplicated for both invoices and carts.

That is the idea. I look forward to your thoughts on this.


Shopping Cart With MySQL
We currently use romancart for our online shopping cart, this has been
fine for the first year of business but once an order is placed we have
to enter it into our own database


Does any one know and good shopping card that use mysql so all order
can be put directly into the database.

Query For Categories Not Added To Cart
Greetings,
I'm trying to write a query for my wishlist application (just a shopping cart, really) that will check for any categories that have not had items added from them to the wishlist. Could someone help me with the query syntax?

I have my wishlist table with columns: cartId, cookieId, itemId, qty; and my items table with columns: id, parent, name (I'm using the adjacency list model).

PHP

# check for items in the wishlist
$result = mysql_query("select * from wishlist inner join items on wishlist.itemId = items.id where wishlist.cookieId = '" .$_SESSION[wishId]. "'");

# for items in wishlist, check if match with category number, and if not, display link to category
while($row = mysql_fetch_array($result))
  {
    if($row['id'] !== "17") {
    echo '<a href="products.php?page=17">Tables & Chairs</a>'
    }
  }

I *think* the issue probably is in the line if($row['id'] !== "17") and I should be using a different method to check for matches between wishlist items and categories, is this right? Can someone point me in the right direction?

Direct Add Cart Url Link Not Working
I am doing a simple shopping cart which works (code below)BUT...

when i click on the add item to cart link it works and produces a url:

blah/cart.php?action=add_item&id=4&qty=1

Now, what I would like to do is link the url to a button in flash so that people can click directly and add a specific item. I know how to do that but when i try to change the
add_item&id=1 to add_item&id=2 or whatever, the thing does not work properly. Code:

Top 5 Owners Who Have The Most Products
I have a table with the following fields:

ProductId, Price, OwnerID

along with a set of data:

101, 3.50, 1
102, 5.00, 3
103, 2.00, 3
104, 6.70, 1
105, 6.20, 1

Meaning that each owner can has more than one product.

I am now stuck at how can I pull out and display the top five owners with the most number of products owned? Based on the above data set, I would like to stat that Owner 1 has 3 products, followed by owner 3, who has 2 products etc. up to the top 5 owners only.

MySQL version: 4.1.21

Installing Products
I don't know much about mySQL but I want to start to work with it.
Can you tell me wich software I need to instal and how can I get them? A friend told me about working with toad and mySQL.
And to work with mySQL and PHP, wich software do I need?

Tagging Products MYSql Help
I have created a products tag table (1 to many).

The structure is:
products_id - pk
products_tag - pk
tally

eg data:
1,sports,1
1,football,1
2,sports,1
2,baseball,1
3,sports,1
3,football,1
4,sports,1
4,basketball,1
5,garden,1
5, hardware,1

What I want to do, for example, is on a tag page (say 'sports'), do a query, that lists, products that are tagged 'sports' are also tagged....(say football, baseball,basketball)..

So on the 'sports' tag page above it lists all products that have been tagged 'sports'..and then at the bottom, it lists DISTINCTly the alternative tags (football, baseball, basketball) used to describe the products in sports.


Show Orders Where All Products Are Available
I have the standard order, and order_products tables, linked by order_id fields.  Order table holds the customer id, order date etc, and the order_products table holds the product_id, quantity and order_id.

I have been asked to find a way to produce a report on all orders where they are completable.  For example, given the fact that product A,B,C and D are in stock, only return those orders which have ordered one or more of those products.  If the order also includes product E, currently not in stock, then don't return that order number.

Retrieve Top 100 Selling Products
I have table of categories, and a list of products with a counter in the product table.  The counter in the product table gets incremented by 1 each time that product is sold.  How do I get the top 100 selling products in each category.

Data Design For Products
I'm designing an E-commerce site and I have a problem with how to organize the products and their attributes.

I need to store a product, (normal stuff like name & description), but I also need to store sizes and colours, and not every product will have every size or colour. I may also need to store other attributes, (width for example), and I'd like it to be fairly open ended. The main product will be different kinds of shoes, but it is possible that there will be other types of products sold.

So, I'm trying to wrap my head around how this should work. I think that this must be a problem that's been solved many times before so I don't want to re-invent the wheel. I found this suggestion (http://webdesign.ittoolbox.com/groups/technical-functional/asp-l/relational-database-design-and-implementation-51842) and am thinking that it may work well. Are there some articles or anything that I should read.

Compare Products From Shopping Carts
cart_table:
cart_id
item_id
quantity

I want to compare the items from two shopping carts. For example, compare shopping cart 123 and 382 like:

Contents of 123:
item_ID________quantity
299188________9
299291________2
299004________5

Contents of 382:
item_ID________quantity
299122________9
299291________1
299004________8

Difference between 123 and 382:
item_ID________quantity
299188________x
299291________x
299004________x


Join - 3 Tables - Products Categories
Simplifying my problem:
I have 3 tables.

Code: ( text )

Huge Database With 800 Customers / 3.500 Products
I need for a webshopproject all customers with all products in one
file. I have already both informations in two different ones. Pls.
find below the relevant info.
product
ProductID|pricegroup|
customer
knr|pricegroup|%calc|price

It is possible that one product is related to 800 customers with
800 different prices. In my thinking the new DB should look like
pricefile
ProductID|knr|pricegroup|%calc|price.

Correct Table Structure To Cross Sell Products
I have implemented an e-commerce system which is currently being used in about 10 different websites. I would like to develop to second phase to it by implementing cross sell/up sell so that I can display related products to the customer when viewing one item.

The way I’m planning on forming the relationship between two liked products is by creating a table that with the following fields:

Create table cross_sell…{
Id int..,
Product_id int.., - this will hold the ID of the source product I want to compare
Related_product_id int – this will hold the ID of the product that is similar to Product_id
}


I’m not so sure if this is the industry standard for creating cross-sell/up-sell relation, so I would greatly appreciate it if you provide me with some input.

Linking Stores With Products (was "MySQL Dificult Code")
I have a MySQL database dificult (for me anyway..).
I have a ecommerce site with a table of products.
Now I want to create a new table with a list of stores where clients can find near home those products. In a limit situation I can have a list of all products indexed in one store (and the products list is increasing everyday, the same with the list of store...). I want this feature to be capable of introdution of new products in a store that already as several products.
My idea is to develop 3 tables:

Products
+----+-----------+--------------+
| id | name | description |
+----+-----------+--------------+
| 1 | xxxxx | aasa... |
| 2 | vvvvvv | asasasasas... |
+----+-----------+--------------+


Stores
+----+-------------------+-----------+
| id | store | address |
+----+-------------------+----------+
| 1 | aaaaa | street1... |
| 2 | bbbbbb | street2... |
+----+-------------------+-----------+



Linking stores with products
+----+-------------------+-------------+
| id | store id | products id |
+----+-------------------+-------------+
| 1 | 1 | 1;2 |
| 2 | 2 | 2 |
+----+-------------------+-------------+

Can I implement this in MySQL? I mean how can (if I can) place several ids in a field? and how can I do the query in order to list the products inside a store?
Hope that I made my self clear. If not please do not hesitate to request further information.




Most Products Flagged In The Past 24 Hours (was "Query Help")
I need a little help setting up the logic behind a query. Here is the situation:

I have 2 tables. Product table. Flag table (flagID, productID, date_flagged). Users can flag products. *date_flagged is a INTEGER unix timestamp.

I would like to be able to setup a query where I can pull up products that have been flagged the most in the past 24 hours.

At first I figured I would COUNT() the flags and GROUP BY productID, but this of course will only give me the most flagged all time, and because of the grouping by productID will not allow me to filter it by date.

#1136 - Column Count Doesn't Match Value Count At Row 1
I'm getting the above error with the following SQL Statement. I cannot seem to find the error in the code. The select statement does pull multiple rows.

I'm using my SQL version: 4.1.19

Any clue on why this isn't working?

INSERT INTO `Grants` ( `Project_Code` , `Grant_Code` , `Fiscal_Year` , `Capital` )
VALUES (
(
SELECT Project_Code, Grant_Code, FY, SUM( Capital )
FROM Grants_Temp
WHERE Project_Code = 'OSUT'
AND FY = '2006'
AND Claim_Month = '072006'
GROUP BY Grant_Code
ORDER BY Grant_Code
)
)

Multiple Languages, Multiple Products
I posted before and was helped immensely. I am designing a multilingual-capable site where each product may (or may not!) have a description in the user's chosen language. In this case, I want to get the default language description.

Say my product descriptions table has fields prod_id, language, and description.

When I have a single product and I want its description, I can retrieve it as follows:

Column Count Doesn't Match Value Count At Row 1
I am getting the following error when I run my query.
Column count doesn't match value count at row 1

I have looked up this error and have checked and I appear to have the right number and names in my query. In the DB table I have 34 columns, and that is what I have in the query.

The last 4 in the query are for the names of the images being uploaded, but am not sure how this all works, so I don't know if I need them, but have them there until I know for sure. Comment, Purchase and Remarks are not used in this form, but I have added them so everything is being shown in the query. Code:

Count(*) As Count And TotalCount Of Count
data in myTable1

(n) country_id
(1) 3
(2) 1
(41) 1
(5) 2
(6) 3
(7) 4

data in myTable4

(time) param
(10 : 10) c=4
(10 : 12) c=2
(10 : 30) n=41&k=5
(10 : 35) c=1
(10 : 37) n=5
(10 : 50) c=2
(10 : 54) c=2
(10 : 55) n=1&cate=6
(11 : 12) c=2
(11 : 15) n=7
(11 : 20) c=1
I have data in myTables like the above.

I have the following code.

code

(select left(time,2) as hour, count(*) as count
from myTable4
where

left(param,1)='c'
and
substring(param,3,1)=2

group by left(time,2)
)
UNION all
(select left(time,2) as hour, count(*) as count
from myTable4,myTable1

where
left(param,1)='n'
and
substring(substring_index(param, '&', 1) ,3)=myTable1.n
and
myTable1.country_id=2
group by left(time,2)
)

order by hour
And the code above produces the following result.

result

(day) count
(10) 3
(10) 1
(11) 1
The following would-be code doesn't work correctly, but it will show what I want.


would-be code

(select left(time,2) as hour, count(*) as count,
sum(count) as totalCount
from myTable4
where

left(param,1)='c'
and
substring(param,3,1)=2

group by left(time,2)
)
UNION all
(select left(time,2) as hour, count(*) as count,
sum(count) as totalCount
from myTable4,myTable1

where
left(param,1)='n'
and
substring(substring_index(param, '&', 1) ,3)=myTable1.n
and
myTable1.country_id=2
group by left(time,2)
)

order by hour
And the following is my target result.

target result

(day) count totalCount
(10) 3 5
(10) 1 5
(11) 1 5


Difference Between Count(*) And Count(1)
What is the difference between count(*) and count(1)?

Difference Of Count(*) From Count(1)
Is it true that count(1) is more efficient than count(*)? They say that count(*) still goes through all the records without needing to.

How To Fix &quot;Column Count Doesn't Match Value Count At Row 1&quot; ?
I've been trying to install a portal to my installation of Invision Power Board, and I keep coming up with this problem. I post here for two reasons: firstly is that no one is responding over at the portal creator's thread and I need help, second is that this error looks like a mySQL error that could be solved without the need of the creator if possible.

Here is the error returned:

mySQL query error: INSERT INTO `ibf_portal_box` VALUES (1, 'lang.last_topics', '<box><menu>10,20,30,50</menu>
<topics_show>10</topics_show>
<order>last_post</order>
<exforums></exforums>
</box>', 0, 'middle', '*', 0, 4, 'last_topics', '1')

mySQL error: Column count doesn't match value count at row 1
mySQL error code:
Date: Tuesday 21st of December 2004 08:43:10 PM

I am very new to mySQL and don't really understand it. What is wrong with the above that it is trying to insert, and how is it fixed?

Count(*) In A Select Returns &quot;1&quot;. It Should Behave Like Select Count(*)
i'm trying to make a query work properly but I got lost:

SELECT *,count(*)
FROM cancons c, musics m, discos d, r_discos_cancons rdc
WHERE c.c_id_music = m.m_id
AND rdc.rdc_id_disc = d.d_id
AND d.d_id_music = m.m_id
AND m.m_id = 24
GROUP BY c_id

note:
cancons (ca) = songs (en)
discos (ca) = cd's (en)
music (ca) = musician (en)

don't worry for the WHERE part. i need it because of the foreign keys.
this query returns a table with the title of the song and some more data. on the right side, I get another column called "count(c_id)" with the number "1" in it for each row. That's supposed to be due to the "group by" clause, I think.

I'd like to get the table with the songs, as usual, and, with the same query, I'd like to get the total number of rows selected.

How To Get The Row Number
I'm new to MySQL, I started two weeks ago. I always run in to problems, everyone does, but I often find the solutions via Google. This time I didn't, I've tried to find a solution for more than an hour now but without success. It's a simple problem, how do you get the row number from a mysql_query() result? This is my code (PHP), stripped down a little:

$result = mysql_query( "SELECT * FROM articles WHERE id='$id'" );
mysql_result( $result, <need the row number here>, "name" );

I can't just use the id field, because when you delete a row, the rows are moved up but the id value isn't changed.

Getting The Number Of A Row?
How can I get the number of a row that I have queried using PHP? I know mysql_num_rows returns how many rows there are but say I want to select the 4th row out of 5, how would I get the number 4?

Id Number
in a database i have a field for an id number. i've seen in other databases i've updated, where the id automatically fills in the field wiht the next highest number.

Getting A Non-zero Number
A table has a series of fields (e.g. id1, id2, id3, id4, etc) that can contain an unsigned integer. The default value for these fields is 0. I am trying to find some way to use built in MySQL functions to determine which is the last field in that series to have a non-zero number and return that number within the results instead of having to do it in the results using a PHP loop.

In other words, if id4 is 0 and id3 is greater than zero then I want id3 but not id2 or id1 regardless of their values, and so on and so forth.

Number Of Row
I have 2 columns. When I do sql query:

 SELECT username FROM users ORDER BY level + 0 DESC 

so it returns me something like this:

username ----------- level
john------------------99
paul------------------90
tom------------------80
nick------------------60

I want php to tell me at what row specific user is located in. Let's say I choose user JOHN, so php would tell me 1 because john has the highest level, so he's the first in the sorted. If I were to choose nick, php would tell me #4

Get The Row Number
I have a site that allows searching for names in an address book. Search terms typed by the user become elements in a where clause for mySQL. So, if you search for "Jason", the query would be: (select * from addresses where first like '%Jason%'). Paginating the results to be displayed n at a time on a page is handled by php.

However, what if I want this: to search for "Jason" and have ALL the records in the records in the address book returned, but to jump to the first page that contains "Jason". In other words, instead of filtering the results with a search, I want ALL the results and the STARTING POSITION in the list of the result I'm interested in. Can this be done with mySQL? Can I form a query that would give me ALL the address book records (select * from addresses), while at the same time giving me the position in the list of ALL records that matches the first row from a search (select * from addresses where first like '%Jason%')

I can think of a convoluted way to do it in php, but I'd much rather do it with mySQL.

Row Number
I have selected a row of a table by using select query. How can I know the row's number of that row

ID Number
I have to use MYSQL to set up a database. I have to give the user an ID number that should be 6 digits long and MUST be system generated. I have searched through the manual and it just mentioned the function mysql_insert_id() that it may be used in other case not mine. The (auto_increment) value doesnt work as well.

Row Number
I am using PHPmyAdmin
If I create a table and the rows x,y,z.. How do I know what row number x,y and z is?
I have a update script but it work with the rows? So I need to know the rows numbers.

ID Number
I have a field called post_titles and the primary key is called post_id.
Let's say I want to retrieve 5 random entries from this table, but I want to be able to sort the 5 entries by their post_id. How can I do so? I've tried the following query but it doesnt work.
PHP Code:





 $sql2 = "select * from postings order by RAND() order by post_id DESC"; 

Row Number
I already searched around on mysql.com, but i found nothing. I want to know if there is a way to get the row number for a certain entry. For example, say you have this database

ID                 Name

234                Joe
345                Bob
678                Susan

I want to be able to find out what row number the ID = '345' falls on. Is this possible?

Search By Number
I have a question regarding a mySQl search

Basically I have a list of numbers stored in a field in my database representing different features of a venue

For example 1,16,17,10,8,13,

Now I want to give my client the ability to look for specific venues with a specific feature by typing in the number for the feature they are looking for, ie if they want to find all venues with a bar and 1 = bar then they enter 1 into the search and are presented with all the venues with a bar

Now my problem is if I do a search for &#391;' using LIKE then and a wild card (ie %1%) I will get all the features that have a 1 (ike 16, 17, 10, 13). Is there anyway I can write a search that will only return venues that just have the number 1?

Also the numbers can appear in ANY order so it might be 10,8,26,1,32 ....

Get Number Of Rows Above A Row
I have one table with a some user info, and a column with the users points. I would like to fetch the user data and sonehow calculate in which place that user is. Basically, the number of users that has more points than the user.

An exampel of the table and data:


+----+---------+--------+
| id | name | points |
+----+---------+--------+
| 1 | chris | 1000 |
| 2 | john | 900 |
| 3 | jeff | 800 |
| 4 | jay | 700 |
| 5 | michael | 600 |
| 6 | eric | 500 |
| 7 | stuart | 400 |
+----+---------+--------+

And an example of the data set I would like:

+----+---------+--------+----------+
| id | name | points | position |
+----+---------+--------+----------+
| 1 | chris | 1000 | 1 |
| 3 | jeff | 800 | 3 |
| 6 | eric | 500 | 6 |
+----+---------+--------+----------+

I'm not sure how to do this in one query, and I'm not even sure that it's possible, but if it is I sure would like to know how.

I'm using MySQL 4.1.16.

Number Of Tables
just a quick question, some database designs i've seen seem to want to compress everything into as little tables as possible, whereas others can go into a hundred rows for a system that doesn't seem to be that much more complex.

A wordpress database is fairly tiny at only a handful of tables, whereas zen cart has about 100+ from memory, and yet I don't see either one as significantly more complicated than the other (although I don't know much about Zen Cart).

I do understand the concept of keeping data in logical divisions, but I wonder how far one should push that towards keeping the number of tables to a minimum - no doubt purely just from a human management standpoint....

Return The Row Number
I have a problem. I need to rank data from highest to lowest, then return the value of what rank a certain piece of data is

For example

id value
1 20
2 80
3 60
4 120
5 10

So I need to make a query that organizes this by value

4 120
2 80
3 60
1 20
5 10

Then if my query says "for ID 1" it will return the value "4" since it is ranked 4th.

Is there any easy way to do this with a query? I am using PHP as my programming language, if theres a way of doing it with PHP can someone direct me to it?


Copyright © 2005-08 www.BigResource.com, All rights reserved