Forums / General Questions / SQL question

SQL question

Locked

Started by digitalshadow on 10 Nov 2010, 7:47 PM · Views: 4,491

Results 1 to 20 of 68
This thread is locked. New replies are disabled.
10 Nov 2010, 7:47 PM
#1
digitalshadow avatar

digitalshadow

Totally Zenned

Join Date:
Mar 2009
Posts:
616
Plugin Contributions:
0

SQL question

Is there a way to query the database to list all the different entries from a field in a table and ignore duplicates?

For example a table with the following entries

cat
dog
cat
cat
rabbit
dog
elephant
cat
cat
dog
dog

would output a list reading

cat
dog
rabbit
elephant

10 Nov 2010, 7:56 PM
#2
kobra avatar

kobra

Black Belt

Join Date:
Aug 2005
Location:
Arizona
Posts:
31,500
Plugin Contributions:
4

Re: SQL question

For what reason???
And is this to be included as some feature in the cart or only for you?

If in the cart please explain in much more detail what it is you are seeking

Zen-Venom Get Bitten

10 Nov 2010, 8:18 PM
#3
digitalshadow avatar

digitalshadow

Totally Zenned

Join Date:
Mar 2009
Posts:
616
Plugin Contributions:
0

Re: SQL question

Each product I sell has a model code, this code is often shared by more than one product.

What I wanted was to list all the different model codes that I have on the store so someone can see at a glance if we stock products for that model code. If they see their model code they can click that code and the site will search the website for that code.

If you imagine the example I gave earlier and you sold products for different animals, different products could all be found under "cats" I would like a list to be generated for all the different animals I sold products for.

so even though you may have 1000 products there may only be 15 different animals, so the list i would like generated would be 15 items long listing the different animals, not 1000 items long listing each products animal, resulting in many duplicates.

10 Nov 2010, 8:20 PM
#4
kobra avatar

kobra

Black Belt

Join Date:
Aug 2005
Location:
Arizona
Posts:
31,500
Plugin Contributions:
4

Re: SQL question

Each product I sell has a model code, this code is often shared by more than one product.
Shared??

Is this the same product or a totally different product with the same code?

Zen-Venom Get Bitten

10 Nov 2010, 8:52 PM
#5
digitalshadow avatar

digitalshadow

Totally Zenned

Join Date:
Mar 2009
Posts:
616
Plugin Contributions:
0

Re: SQL question

different products.

If you imagine

Product 1 = Water Bowl - (code = dog)
Product 2 = Bed - (code = dog)
Product 3 = toy - (code = cat)
Product 4 = ball - (code = rabbit)
Product 5 = ball - (code = dog)
Product 6 = water bowl - (code = cat)

list generated

dog
cat
rabbit

instead of

dog
dog
cat
rabbit
dog
cat

I want it to list all individual entries in the table field.

10 Nov 2010, 8:58 PM
#6
kobra avatar

kobra

Black Belt

Join Date:
Aug 2005
Location:
Arizona
Posts:
31,500
Plugin Contributions:
4

Re: SQL question

I suggest that you create/arrange your products in master categories of dog, cat, & rabbit then link them to other areas
https://www.zen-cart.com/tutorials/index.php?article=216

Zen-Venom Get Bitten

10 Nov 2010, 9:40 PM
#7
digitalshadow avatar

digitalshadow

Totally Zenned

Join Date:
Mar 2009
Posts:
616
Plugin Contributions:
0

Re: SQL question

Not applicable.

Just trying to find a way to query the database to return all the info from a field and filter out duplicates.

11 Nov 2010, 9:10 PM
#10
digitalshadow avatar

digitalshadow

Totally Zenned

Join Date:
Mar 2009
Posts:
616
Plugin Contributions:
0

Re: SQL question

I'm having trouble writing the php code, can anyone help?

13 Nov 2010, 2:49 PM
#11
digitalshadow avatar

digitalshadow

Totally Zenned

Join Date:
Mar 2009
Posts:
616
Plugin Contributions:
0

Re: SQL question

Someone must be able to help?

13 Nov 2010, 3:11 PM
#12
kobra avatar

kobra

Black Belt

Join Date:
Aug 2005
Location:
Arizona
Posts:
31,500
Plugin Contributions:
4

Re: SQL question

Someone must be able to help?
Whhile you know what it is that you want, it is not clear to others

Your OP asked for one thing, and this has escalated into something else

i.e.
What page you want this
What customer action will trigger it
What file(s) are you thinking of adding it to
How it is to be displayed
etc etc

As I stated earlier
If in the cart please explain in much more detail what it is you are seeking

We can not read your mind and even though it is clear to you it is not from what you have provided clear to anybody else

Zen-Venom Get Bitten

13 Nov 2010, 7:12 PM
#13
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,874
Plugin Contributions:
8

Re: SQL question

There may not be enough information in the OP's posts to completely create his functionality, but the SQL to get the info requested from the db should be straightforward for those who are good with MySQL.

13 Nov 2010, 7:22 PM
#14
kobra avatar

kobra

Black Belt

Join Date:
Aug 2005
Location:
Arizona
Posts:
31,500
Plugin Contributions:
4

Re: SQL question

gjh42:

There may not be enough information in the OP's posts to completely create his functionality, but the SQL to get the info requested from the db should be straightforward for those who are good with MySQL.
Fully agree but almost knew that just the sql was not what was wanted
And why I asked
kobra:

And is this to be included as some feature in the cart or only for you?
And the poster figured out that they also needed some php/mysql code but no idea given as to what or where this was to be used/displayed
DigitalShadow:

I need a php code to make that work

Zen-Venom Get Bitten

14 Nov 2010, 12:24 PM
#15
digitalshadow avatar

digitalshadow

Totally Zenned

Join Date:
Mar 2009
Posts:
616
Plugin Contributions:
0

Re: SQL question

I've got the sql code to work in phpmyadmin, what i need is the code to work in the shop front.

SELECT DISTINCT products_model_number FROM zen_products_description

All i need is to display the results on the shop front.

What code should i write to that that will display on the shop?

14 Nov 2010, 3:52 PM
#16
kobra avatar

kobra

Black Belt

Join Date:
Aug 2005
Location:
Arizona
Posts:
31,500
Plugin Contributions:
4

Re: SQL question

What code should i write to that that will display on the shop?
Back to my original post - you will have to much more specific about where you want this displayed as "display on the shop" is a lot of different pages

Zen-Venom Get Bitten

15 Nov 2010, 2:32 PM
#17
digitalshadow avatar

digitalshadow

Totally Zenned

Join Date:
Mar 2009
Posts:
616
Plugin Contributions:
0

Re: SQL question

An EZPage would probably be the best place to put the generated list.

15 Nov 2010, 2:35 PM
#18
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,874
Plugin Contributions:
8

Re: SQL question

Standard Zen Cart ez-pages don't handle PHP, so you would have to do some tricky customizing to get that to work.
Define pages do handle PHP, so you might make a new page for your content - see the "About Us page" mod in Free Addons.

15 Nov 2010, 2:54 PM
#19
digitalshadow avatar

digitalshadow

Totally Zenned

Join Date:
Mar 2009
Posts:
616
Plugin Contributions:
0

Re: SQL question

Ok, lets say I create a new define page for the list, which isn't a problem.

15 Nov 2010, 3:00 PM
#20
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,874
Plugin Contributions:
8

Re: SQL question

Search in Tools > Developers Toolkit for
$db->Execute(
and go to some of the files listed. You will see examples of SQL queries being handled in various ways.

When you assign the result to a variable, you can then use or display that variable's contents in standard PHP ways.