I have a project almost (or so I thought!) completed but now I need to add a column to a table which is throwing everything off. Right now I have TableMain which queries TableA. I need to add a column (bit) to TableMain and then run a query that is basicallyif (bitColumn) query TableB, else query TableA.
for each row.
TableA contains a listing, TableB is groups where each group contains multiple items from TableA. The columns I want to pull from each table have the same names (ie. regardless of bitColumn, I want TableA.Name or TableB.Name)
I'm not sure how I can go about doing this, I greatly appreciate any help.
Can you please give following details.
Primary key and Fkey in each table
Example input and output data
|||
Paper (id(pkey), name, code, description, clipID(fkey), etc) <= Think of these as a sheet of paper.
Clip(id(pkey), name, description) <= Think of these as a group of papers paperclipped together.
Cart(id(pkey),user,paperID(fkey),qty)
if someone tries to order a paper that has a clipID, they are forced to buy the entire paperclip. My shopping cart works for regular papers, and I have it notifying the user that they are really ordering the paperclip. What I'm stuck on is displaying the shopping cart, etc.
Shopping Cart: Paper1 + Paper2 + Clip1, where Clip1 has Paper3 and Paper4.
I don't know how to make the cart show Clip1 or whether I should just force add each individual item to the shopping cart (making editing qtys difficult), etc. I was thinking about adding an isClip column to the cart and then the stored procudure returns Paper.name or Clip.name depending on whether isClip is 0 or 1... so that it would be
Cart(id(pkey),user,paperID(fkey),qty,isClip)
I don't know how I would write that query, and I'm not sure its even the best design
|||I would design like this.
Category table
Products Table
Product details table
In category table : categories are paper and clip
In products table: Different papers(only papers not associated with clips) and clips
In product Details table: Papers that goes with clips
Papers that are associated to clips are only sold as a bunch
When user click on paper, show paper details and when user clicks on clip, show all the papers associated to clip by querying from product details table.
With all of the code already written, I'd like to avoid a complete design restructuring.
Motley: this is what I started working on last night until I ran into a problem. Each paper belongs to a certain topic, and in the catalog are listed under topic headings as the sp returns with order by topic. The papers in a clip do not need to (and rarely will) belong to the same topic. I could have a Clips topic that I display first and then the individual topics, but it would involve checking each clip and seeing how many papers reference it; or perhaps adding a Count field to the Clips table? I would still then need to figure out a way to return the correct ordering...I'm open to hearing other suggestions...
|||edit...yeah, that doesn't work, never mind...
SELECT
clip.id, clip.name, clip.description,
ISNULL(paper.topic,'CLIP')
FROM
clips clip
LEFT JOIN
papers paper
ON
clip.id = paper.kitID
ORDER BY
topic
|||Basic query:
SELECT c.id as ClipID,c.Name as ClipName,c.Description as ClipDescription,p.id as PaperID,p.name as PaperName, p.code as PaperCode, p.Description as PaperDescription, etc
FROM clip c
JOIN paper p ON (c.PaperID=p.id)
Return clips and how many papers are attached:
SELECT c.id AS ClipID,c.name,c.description,COUNT(*)
FROM clip c
JOIN paper p ON (c.PaperID=p.id)
GROUP BY c.id,c.name,c.description
Of course this assumes that each paper has a clip associated with it.
With your original structure:
SELECT s.*,name,description
FROM cart s
JOIN clip c ON (s.ID=c.id and s.isClip=1)
UNION
SELECT s.*,name,description
FROM cart s
JOIN paper p ON (s.ID=p.id and s.isClip=0)
OR
SELECT s.*,CASE WHEN c.id IS NOT NULL THEN c.name ELSE p.name END as Name, CASE WHEN c.id IS NOT NULL THEN c.description ELSE p.description END as Description
FROM cart s
LEFT JOIN clip c ON (c.id=s.id and s.isClip=1)
LEFT JOIN paper p ON (p.id=s.id and s.isClip=0)
sql
No comments:
Post a Comment