do they have to have a common primary key?
Not necessarily. You can get a CROSS JOIN of both tables but I am not sure how much help that would be.
|||I have these two tables
id int Unchecked
CategoryID int Checked
ListTitle nvarchar(50) Checked
Blurb nvarchar(250) Checked
FileName nvarchar(50) Checked
ByLine nvarchar(50) Checked
HTMLCopy nvarchar(MAX) Checked
MainStory bit Checked
MainStoryImageFile nvarchar(50) Checked
Publish bit Checked
Date smalldatetime Checked
and
CategoryID int Unchecked
CategoryTitle nvarchar(50) Checked
and i need to get to the categoryTitle along with stuff from the first table. any idea?
|||What does 'Checked' and 'UnChecked' mean? Also post some sample data from each of the tables and expected output, makes it easier for me to work on some data.
|||ooops. checked and unchecked was just the allow nulls check box ticked or not in my table definition. i need to get category name and stuff from the second table shown here. Is this what you mean.
he was not very good he was not very good he was not very good he was not very good he was not very good he was not very good he was not very good</p>
<p><font color="#ff0000" size="5">No 9. Vitas Geralitis</font><br />
he was not very good he was not very good he was not very good he was not very good he was not very good he was not very good he was not very good he was not very good he was not very good</p>
<p><font color="#ff0000" size="5">No 8. Bjorn Borg<br />
</font>he was not very good he was not very good he was not very good he was not very good he was not very good he was not very good he was not very good he was not very good he was not very good</p>
</strong></font>This is the classic board game</p>
<p>No. 9 <strong><font color="#800080">Backgammon<br />
</font></strong>Hope you are good with numbers.</p>
The greatest team of all time. The greatest team of all time. The greatest team of all time.The greatest team of all time.</p>
<p><font color="#800080"><strong>No9. Arsenal<br />
</strong></font>Not a bad outfit now Henry has left. Not a bad outfit now Henry has left. Not a bad outfit now Henry has left. Not a bad outfit now Henry has left.</p>
<p><font color="#800080"><strong>No 8. Liverpool</strong></font><br />
Three times European Champions. Three times European Champions.Three times European Champions. Three times European Champions. Three times European Champions.</p>
<p>in football Fifa rules state that a player must leave the pitch if in need of treatment for an injury which can easily tempt a nervous team into ankle-stamping the opposition's best player minutes before the whistle. Those precious seconds waiting for the ref to bring him back on could prove vital. Those precious seconds waiting for the ref to bring him back on could prove vital. Those precious seconds waiting for the ref to bring him back on could prove vital. Those precious seconds waiting for the ref to him back on could prove vital.</p>
<h3><font color="#800080">No.9 No concessions for disabled people in golf</font></h3>
<p>American Casey Martin was no world-class golfer, but in the late 90s he was doing reasonably well on the PGA's minor league Nike Tour. Martin had a birth defect in his leg attributed to Klippel Trenaunay Weber syndrome, which made walking painful. But the PGA wouldn't let him use a golf cart to get round. So he sued them.</p>
<h3><font color="#800080">No.8 No coaching mid-match in tennis</font></h3>
<p>Think of every sport and you'll see a ranting manager or trainer somewhere on the sidelines. Except tennis, where players have to wait until they change ends for a hurried chat with the man who takes 15 per cent. Even toilet breaks are escorted, to ensure the rule isn't broken.</p>
<h3><font color="#800080">No.7 Not being allowed to over-celebrate in football</font></h3>
<p>In a career that spanned over 500 matches, Alan Hansen scored just 14 goals. True, he was a defender, but had the day ever come that the dour Scot popped one in for his national team (0 goals from 26 games), he'd have gone absolutely bananas. And if today, probably booked.</p>
<h3><font color="#800080">No.6 Taken off for treatment</font></h3>
<p>in football Fifa rules state that a player must leave the pitch if in need of treatment for an injury which can easily tempt a nervous team into ankle-stamping the opposition's best player minutes before the whistle. Those precious seconds waiting for the ref to bring him back on could prove vital. Those precious seconds waiting for the ref to bring him back on could prove vital. Those precious seconds waiting for the ref to bring him back on could prove vital. Those precious seconds waiting for the ref to him back on could prove vital.</p>
<h3><font color="#800080">No.5 No concessions for disabled people in golf</font></h3>
<p>American Casey Martin was no world-class golfer, but in the late 90s he was doing reasonably well on the PGA's minor league Nike Tour. Martin had a birth defect in his leg attributed to Klippel Trenaunay Weber syndrome, which made walking painful. But the PGA wouldn't let him use a golf cart to get round. So he sued them.</p>
<h3><font color="#800080">No.4 No coaching mid-match in tennis</font></h3>
<p>Think of every sport and you'll see a ranting manager or trainer somewhere on the sidelines. Except tennis, where players have to wait until they change ends for a hurried chat with the man who takes 15 per cent. Even toilet breaks are escorted, to ensure the rule isn't broken.</p>
<h3><font color="#800080">No.3 Not being allowed to over-celebrate in football</font></h3>
<p>In a career that spanned over 500 matches, Alan Hansen scored just 14 goals. True, he was a defender, but had the day ever come that the dour Scot popped one in for his national team (0 goals from 26 games), he'd have gone absolutely bananas. And if today, probably booked.</p>
<h3><font color="#800080">No.2 Taken off for treatment</font></h3>
<p>in football Fifa rules state that a player must leave the pitch if in need of treatment for an injury which can easily tempt a nervous team into ankle-stamping the opposition's best player minutes before the whistle. Those precious seconds waiting for the ref to bring him back on could prove vital. Those precious seconds waiting for the ref to bring him back on could prove vital. Those precious seconds waiting for the ref to bring him back on could prove vital. Those precious seconds waiting for the ref to him back on could prove vital.</p>
<h3><font color="#800080">No.1 No concessions for disabled people in golf</font></h3>
<p>American Casey Martin was no world-class golfer, but in the late 90s he was doing reasonably well on the PGA's minor league Nike Tour. Martin had a birth defect in his leg attributed to Klippel Trenaunay Weber syndrome, which made walking painful. But the PGA wouldn't let him use a golf cart to get round. So he sued them.</p>
</font></font>he was the master and this goal proves it he was the master and this goal proves it he was the master and this goal proves it he was the master and this goal proves it he was the master and this goal proves it</p>
<p><font color="#800080" size="5"><strong>No 9. Maradonna</strong></font><br />
This goal sank England in the 1998 World Cup finals This goal sank England in the 1998 World Cup finals This goal sank England in the 1998 World Cup finals This goal sank England in the 1998 World Cup finals</p>
<p><strong><font color="#800080" size="5">No 8. Zico</font></strong><br />
Another Fantastic goal Another Fantastic goal Another Fantastic goal Another Fantastic goal Another Fantastic goal Another Fantastic goal Another Fantastic goal Another Fantastic goal Another Fantastic goal</p>
<p><strong><font color="#800080" size="5">No 7. Zico<br />
</font></strong>Another Fantastic goal Another Fantastic goal Another Fantastic goal Another Fantastic goal Another Fantastic goal Another Fantastic goal Another Fantastic goal Another Fantastic goal Another Fantastic goal</p>
<p><font color="#800080"><font size="5"><strong>No 6. Pele</strong><br />
</font></font>he was the master and this goal proves it he was the master and this goal proves it he was the master and this goal proves it he was the master and this goal proves it he was the master and this goal proves it</p>
<p><font color="#800080" size="5"><strong>No 5. Maradonna</strong></font><br />
This goal sank England in the 1998 World Cup finals This goal sank England in the 1998 World Cup finals This goal sank England in the 1998 World Cup finals This goal sank England in the 1998 World Cup finals</p>
<p><font color="#800080"><font size="5"><strong>No 4. Pele</strong><br />
</font></font>he was the master and this goal proves it he was the master and this goal proves it he was the master and this goal proves it he was the master and this goal proves it he was the master and this goal proves it</p>
<p><font color="#800080" size="5"><strong>No 3. Maradonna</strong></font><br />
This goal sank England in the 1998 World Cup finals This goal sank England in the 1998 World Cup finals This goal sank England in the 1998 World Cup finals This goal sank England in the 1998 World Cup finals</p>
<p><font color="#800080"><font size="5"><strong>No 2. Pele</strong><br />
</font></font>he was the master and this goal proves it he was the master and this goal proves it he was the master and this goal proves it he was the master and this goal proves it he was the master and this goal proves it</p>
<p><font color="#800080" size="5"><strong>No 1. Maradonna</strong></font><br />
This goal sank England in the 1998 World Cup finals This goal sank England in the 1998 World Cup finals This goal sank England in the 1998 World Cup finals This goal sank England in the 1998 World Cup finals</p>
</strong></font><font face="Courier New">Small but perfectly formed, Sir Richard’s ‘tache was like many Kiwi attempts to keep up with the cricketing powerhouses, not bad but a little short of what is required for world class.<br />
Sir Richard <br />
<br />
<font color="#0000ff" size="5"><strong>9. Jack Russell:</strong></font><br />
Eccentric wicket-keeper who could tell to within a matter of seconds how long his Weetabix had been soaked in milk for. Perhaps England’s greatest ever gloveman, now an artist, his moustache was as bedraggled as his trademark floppy hat.<br />
<br />
8. Clive Lloyd:<br />
A powerful batsman who, as captain from 1974 to 1985, was largely responsible for the West Indies’ extraordinary success. Also a star for Lancashire, the world’s greatest county cricket club. Great man, great ‘tache. <br />
<br />
7. Robin Smith:<br />
Nicknamed The Judge because of his hair, Smith combined his ‘tache with a mullet. The Judge did not wear a grill on his helmet, which meant the bowlers had a clear view of that top lip while he was hammering the fastest of bowling with supersonic cuts and hooks. He visibly enjoyed the regular snatches of chin music he received from the West Indian quicks. <br />
<br />
6. Kapil Dev:<br />
An Indian legend, Kapil Dev was a fine batsmen and the greatest pace bowler his country has ever produced. Competed in an era of great all-rounders, and like Botham and Hadlee he also had a decent ‘tache. <br />
<br />
5. Sir Ian Botham:<br />
The great man had to make an appearance. Not many men can look good with shoulder length, semi-permed hair and a moustache. But Beefy did, plus he managed to be the world’s greatest ever all-rounder at the same time. What a legend! <br />
<br />
4. Graham Gooch:<br />
Goochie ran the full gauntlet of facial hair during his career, from facial hair to full beard via designer stubble and his famous Zapata moustache. He was at his best though when sporting the ‘tache, as his 333 against India would testify. Interestingly, his 333 was boosted by hundreds from Allan Lamb and Robin Smith in a marvellous Test match for hairy-lips. <br />
<br />
3. David Boon:<br />
Tasmania’s all-time favourite cricketing son, Boonie would have held the Australian prize for the greatest cricketing ‘tache, but for the presence of the world beating Big Merv. Rumour has it that he was considered to be featured on the Aussue $5 note but they couldn’t fit his moustache on.<br />
<br />
2. Adam Hollioake:<br />
Perhaps a surprising pick for second place, Hollioake managed to do what other England captain’s have never, that is win an international ODI tournament when he led an inexperienced team to the Champions Trophy in Sharjah. His Aussie background maybe why he sported an incredible handlebar moustache for the start of the 2004 season. <br />
<br />
1. Merv Hughes:<br />
The clear winner by a hairy mile. According to Cricinfo, the big-hearted Australian fast bowler “was a lively character armed with an imposing run-up and delivery action, a classic fast bowlers’ glare down the pitch, a mischievous sense of humour and a moustache of incredible proportions”. Merv’s facial appendage is listed in Wikipedia as one of the all-time leading handlebar moustaches. Today’s “metrosexual” modern cricketers would do well to follow his lippy lead.</font></p>
I presume you're looking at the tables through Database Explorer - and that by Checked/Unchecked you are referring to the "Allow Nulls" column in the Table Editor. So describe your columns instead as "id int NOT NULL" or "CategoryID int NULL" and people will understand you.
To answer your question, you'll be doing an INNER JOIN on the tables by the CategoryID.
However, be careful about your first table's definition where all but the id column are nullable i.e. may be unknown. That immediately looks like poor design. Does every record in the first table have to belong to a category? Then the CategoryID field shouldn't be null in the first table, and you should define a foreign key relationship between it and the Category table (you can do this through point-and-click within the Explorer tool).
If you do so, then when you use the Query Editor in Database Explorer to graphically build your query, you'll see them joined together in exactly the way you want.
|||Assuming your first table (with listtitle, filename etc) is called Category and second table with Title is called CategoryTitle you can write a query as:
SELECT C.id ,C.CategoryID ,C.ListTitle ,C.Blurb ,C.FileName ,C.ByLine ,C.HTMLCopy,C.MainStory ,C.MainStoryImageFile ,C.Publish ,C.Date ,C.TitleFROM Category CJOIN CategoryTitle CTon C.CategoryID = CT.CategoryID
|||
my first table is called shortlist and the second is called categories so can i write the below or do i have to put those Cs in??
SELECT id ,CategoryID ,ListTitle ,Blurb ,FileName ,ByLine ,HTMLCopy,MainStory ,MainStoryImageFile ,Publish ,Date ,CategoryTitleFROM shortlistJOIN CategoryTitleon shortlist.CategoryID = Categories.CategoryID|||
Looking at the structure of your tables it looks like you want to select all the data for a specific category? The only value in the second table that is not in the first is CategoryTitle. So if you want to select all the data from table1 along with the CetegoryTitle Value from Table2... use something like this:
Select a.*, b.CategoryTitle from table1 a
inner join table2 b
on a.CategoryID = b.CategoryID
WHERE a.CategoryID = 'value'
I am assuming you are passing a CategoryID. Remember to replace the table1 and table2 with the table names and the value with the correct param(CategoryID). If this is not what you were trying to do, just send me what you would like to select and what value you are passing into query!
|||Just saw that you posted your table names, so the query will look like this:
Select a.*, b.CategoryTitle from shortlist a
inner join categories b
on a.CategoryID = b.CategoryID
OR:
Select a.*, b.CategoryTitle from shortlist a
inner join categories b
on a.CategoryID = b.CategoryID
WHERE a.CategoryID = 'value'
If you want a specific category, hope this helps and good luck!
|||SELECT id,S.CategoryID,ListTitle ,Blurb ,FileName ,ByLine ,HTMLCopy,MainStory ,MainStoryImageFile ,Publish ,Date ,CategoryTitleFROM shortlist SJOIN CategoryTitle Con S.CategoryID = C.CategoryID
When you have a column that is present in both tables, you will have to specify from which table you want the value retrieved from, otherwise SQL Server will complain
with an "Ambiguous column name CategoryId". So you need to put prefix the column name with the tablename or alias. If the column names are long its a lot of typing.
So we alias it with an acronym that is meaningful. Here I used S for Shortlist and C for CategoryTitle so you can prefix the CategoryId with either "S." or "C.".|||Thank you very much. that worked. What if i want to make the CategoryTitle uppercase? :)|||
This will make the CategoryTitle upper case:
Select a.*, UPPER(b.CategoryTitle) from shortlist a
inner join categories b
on a.CategoryID = b.CategoryID
|||
I would do it at the front end, but there is an UPPER() function if you have to do it in SQL.
No comments:
Post a Comment