I have two tables,Blog table has a FK BlogTagID column point to BlogTag table:
Blog table:
BlogID BlogTagID BlogTitle
1 2 test1
2 1 test2
3 2 test3
BlogTag table:
BlogTagID BlogTagName
1 JAVA
2 .NET
3 PHP
I would like to get the result:
BlogTagName count
JAVA 1
.NET 2
PHP 0
How to get this?Thank you very much!
try this code
select BlogTagName, count(blogid)
from BlogTag bt
left join Blog b on b.blogtagid = bt.BlogTagID
group by BlogTagName
SQL FIDDLE : http://sqlfiddle.com/#!3/356c5/8/0
You can try this also
SELECT BlogTagName,COUNT(BlogTagID) FROM Blog b JOIN BlogTagID bt WHERE b.BlogTagID=bt.BlogTagID GROUP BY BlogTagID;
Related
I admit the title question is convoluted; here is the situation.
I have two tables:
USERS Table
id
name
status
1
Monica
A
2
Ross
A
3
Phoebe
T
4
Chandler
A
5
Rachel
T
6
Joey
A
PERMISSIONS Table
user_id
permission_id
1
32
1
51
4
12
6
2
3
5
5
22
2
18
What I want is a way to delete all rows from the PERMISSIONS table where that user's STATUS is "T" but how would I do that?
I had tried this:
DELETE FROM permissions
WHERE user_id IN (
SELECT id FROM users
WHERE status = 'T'
);
However, SQL Server gives this error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
try apply join permissions and user and where status = 'T'
Example:
DELETE p
FROM permissions p
INNER JOIN users u
ON p.user_id=u.id
WHERE u.status = 'T'
I have a table like
Id RefNumber LotNum
---------------------------
1 Ref-1 10
2 Ref-1 11
Lotnumber:
Lot-Id Lot-Name
-------------------
10 Apple
11 Banana
I need my output to look like this:
Ref-1 Apple,Banana
Please help me - how can I achieve this?
On SQL Server 2017 and later, we can use STRING_AGG here:
SELECT
r.RefNumber,
STRING_AGG(l.[Lot-Name]) WITHIN GROUP (ORDER BY l.[Lot-Id]) AS LotNames
FROM Refs r
LEFT JOIN Lotnumber l
ON r.LotNum = l.[Lot-Id]
GROUP BY
r.RefNumber;
I have the following tables:
Manufacturer: Model range:
IDManufacturer Manufacturer IDModelRange IDManufacturer ModelRange
1 Mercedes 1 1 Benz
2 Audi 2 1 E-Klasse
3 2 TT
4 2 A4
I would like to query the data from both tables and the result to be like:
IDManufacturer+ModelRange
1 Benz
1 E-Klasse
I tried all joins but I couldn't find the right one. Need some help! Thanks
I'm assuming you are trying to find the Models for a specific Manufacturer.
If you are filtering by the ID you don't need the JOIN as stated in the comments.
If you are filtering by the name you can do it with a INNER JOIN. Here is an example:
Declare
#myManufacturerChoice VARCHAR(100) = 'Mercedes';
SELECT
M.IDManufacturer,
mo.ModelRange Manufacturer
from Manufacturer M
inner join [Model range] mo on
M.IDManufacturer = mo.IDManufacturer
where
M.Manufacturer = #myManufacturerChoice
Also a reference link about joins.
Here's the scenario:
I have 3 tables in a SQL Server 2008 database - SERVERS, InstalledPatches and Patchlist.
The SERVERS table has list of servers. InstalledPatches has list of servers and patches installed on them. Patchlist has list of all patches that SHOULD be installed on each server. All patches in PATCHLIST should be ideally installed on all servers in SERVERS table. I am trying to find the patches that are missing.
Sample data:
SERVERS
SERVERID SERVERNAME
-----------------------
1 ABC
.. ..
1500 XYZ
INSTALLEDPATCHES:
SERVERID PATCHID
-----------------
1 1
1 2
2 1
.. ..
1500 1
1500 2
PATCHLIST:
PATCHID PATCHNUMBER
---------------------
1 aaa
2 bbb
3 ccc
4 ddd
.. ..
15 ZZZ
Final report should indicate missing patches:
SERVERID MissingPATCHID
-------------------------
1 3
1 4
1 1500
2 3
2 4
2 1500
..
I have tried to use below query, but cant find all missing patches for each server.
SELECT
A.*
FROM
INSTALLEDPATCHES A
RIGHT OUTER JOIN
PATCHLIST B ON A.PATCHID = B.PATCHID
WHERE
A.PATCHID IS NULL
Any help would be really appreciated.
Thanks.
What about something like?
select s.SERVERID,
pl.PATCHID MissingPATCHID
from SERVERS s
cross join PATCHLIST pl
where not exists (select SERVERID,
PATCHID
from INSTALLEDPATCHES ip
where ip.SERVERID = s.SERVERID
and ip.PATCHID = pl.PATCHID)
I just created this SQLFiddle demo.
Try my query. It works now.
select s.serverid, p1.patchid as MissingPatchID
from [servers] as s
left join patchlist as p1
on 1=1
left join installedpatches as p2
on s.serverid = p2.serverid
and p1.patchid = p2.patchid
where p2.patchid is null
Please feel free to comment on this as I am new and very confused on how to structure this.
I want to create a database of people with interests. I want to record their interests and then see what people have common interests and display them.
I have 3 tables: Person, Interest, InterestType
Person is a table of people
Interest is an interest that a person can have.
InterestType is the name of the interest, say Skiing or Biking. (I separated it because I want all person to use a common typeset of interests)
My setup is as follow:
personTable: id, name, interestID
interestTable: id, interestType, personID
interestType: id, name
How do I get the list of people with the same interest?
I have made a simple model in Access, but you should be able to "translate" this to SQLite without too many problems.
Given:
PersonTable
personId Name
1 Paolo
2 Carla
3 Angelo
4 Franco
5 John
6 Lisa
InterestType
interestId Name
1 Calligraphy
2 Karate
3 Chess
4 Movies
5 Hiking
InterestTable
interestId personId
1 1
2 1
3 1
2 2
3 2
4 2
1 3
2 3
1 5
A simple query sorted by Interest Name and then by Person Name should do the trick:
SELECT interestType.Name, personTable.Name
FROM personTable INNER JOIN
(interestType INNER JOIN interestTable ON
interestType.interestId=interestTable.interestId)
ON personTable.personId=interestTable.personId
ORDER BY 1, 2;
will return:
interestType.Name personTable.Name
Calligraphy Angelo
Calligraphy John
Calligraphy Paolo
Chess Carla
Chess Paolo
Karate Angelo
Karate Carla
Karate Paolo
Movies Carla
If you want to look for a specific interest, just add a where clause:
SELECT interestType.Name, personTable.Name
FROM personTable INNER JOIN
(interestType INNER JOIN interestTable ON interestType.interestId=interestTable.interestId)
ON personTable.personId=interestTable.personId
WHERE interestType.Name="Karate"
ORDER BY 1, 2;
interestType.Name personTable.Name
Karate Angelo
Karate Carla
Karate Paolo
Try this..
SELECT * FROM personTable pt
INNER JOIN interestTable it
ON pt.id = it.id
WHERE it.interestType = "theInterestType";