Join List of words to a table - sql

I have two tables. Table "List" and table "Content". I want to store 6 types of lists. These lists are black,white and grey lists and each of these list contain a few words. Whenever someone notices a new word that should be in one of the lists, then new word should be simply added to the database.
The image below shows you the tables that I use.
I want to refer or join a certain list for instance a list with name: "Blacklist" that has an ListID= 1 with the correct set of blacklistwords, that could have a ContentID=1.
The content is a list of words, but I am clueless as for how I should join the correct list of words(content) to a listID. I don't know how to query this.
The part that is troubeling me is that it is a list of words. So a ContentID =1 has for example the words"Login","Password", "Credential" etc. How do I query it to ListID=1 with the name"BlackList"? And do the same for the other lists?
I think it should look like this.
SELECT ID
FROM List
LEFT JOIN Content
ON LIST.ID = ContenID AND CONTENT.ISDEFAULT = 1
WHERE ListID = 1
This only joins the two ID with each other. How do I join the correct list of words with the correct list? Maybe I am totally missing the point with the query above?
Question: How do I join a set or list of words to a list with a name and ListID?

Once you change this schema, the below query will work
SELECT ListID,ContentID,Words
FROM List
LEFT JOIN Content
ON List.ListID = Content.ListID
WHERE List.ListID = 1

I have considered the schema from the diagrams. Please execute the below query:
SELECT
L.Name AS 'ListName',
C.Words
FROM List L
INNER JOIN Content C ON C.ListID = L.ListID
WHERE
C.Words IN ('Login','Password','Credential')

Related

Access SQL Lookup - Dropdown Columns

I have looked for a similar issue with no luck. Maybe I don't know the right term to search for.
This seems so simple, but I just can't get it after spending many hours trying different approaches.
I have a dropdown to select contracts which shows some ids for related fields. How can I get those IDs to show the value of another column.
SELECT tbl_contracts.ID, tbl_contracts.contract_name, tbl_contracts.firm_id, tbl_contracts.agency_id
FROM tbl_contracts;
image of dropdown
I would like the IDs shown for agency_id and firm_id to list the company_name from their respective table "tbl_firm_agencies" where the tbl_contracts looks them up from. I've tried INNER JOINS but when I do, I can only line items to show when both agency AND firm exist, so my dropdown get cut off quite a bit.
Simply LEFT JOIN to those lookup tables as opposed to INNER JOIN. Adjust table and field names to actual ones in query below. Also, the parentheses are required in MS Access SQL.
SELECT c.ID, c.contract_name, f.firm_name, a.agency_name
FROM (tbl_contracts c
LEFT JOIN tbl_firms f
ON c.firm_id = f.firm_name)
LEFT JOIN tbl_agencies a
ON c.agency_id = a.agency_name;

I want to use CONCAT and exclude and duplicate any duplicate entries

I'm trying to use the CONCAT expression, but also exclude any duplicate entries.
So I'm trying to update a report based on a single process held in our product. The problem is that whoever created the tables that the current report is pulling from is not from a single table. Currently I have found three tables that the report pulls from for one column.
SELECT concat(dbo.t_log_TaskBody.TaskDescription,' ', dbo.t_ezDocument.FileName) as Title
FROM dbo.t_logs_SigDocPrintedEmailed
LEFT JOIN dbo.t_log_Data ON t_logs_SigDocPrintedEmailed.t_ezDataPKid = dbo.t_log_Data.PKid
LEFT JOIN dbo.t_log_TaskBody ON dbo.t_logs_SigDocPrintedEmailed.t_ezSignDocumentQ_PKid = dbo.t_log_TaskBody.DocumentId
LEFT JOIN dbo.t_ezSignDocumentQ ON dbo.t_logs_SigDocPrintedEmailed.t_ezSignDocumentQ_PKid = dbo.t_ezSignDocumentQ.PKid
LEFT JOIN dbo.t_ezArcSigDocQLog ON dbo.t_logs_SigDocPrintedEmailed.t_ezSignDocumentQ_PKid = dbo.t_ezArcSigDocQLog.t_ezSignDocQPKID
LEFT JOIN dbo.t_ezDocument ON dbo.t_ezSignDocumentQ.t_ezDocument = dbo.t_ezDocument.PKID or dbo.t_ezArcSigDocQLog.t_ezDocument = EasyID.dbo.t_ezDocument.PKID
So now that I have one entry that happens to connect to two of the tables I'm pulling from to get a title of a document I end up with the title appearing twice in one box. Is there anyway I can use CONCAT to combine the two tables while keeping it to unique entries, or is there a better way of doing this.
I'll get Something along the lines of:
Title
null
Title Title
null
Title
Title
Based on your sample results, I think you want COALESCE(), not CONCAT():
concat(dbo.t_log_TaskBody.TaskDescription, dbo.t_ezDocument.FileName) as Title
You have no examples where results are actually concatenated.

Microsoft Access joining 4 tables to display specific values from 3 tables but all values from the main table

I have created a database for users to "follow" TV Shows, I need to create a form to display each show and all the relevant information to that specific show.
The four tables I have are as follows:
Shows (Main Table),
Networks,
ShowGenres (Links multiple genre's to one show),
Genres.
The relationships and all fields are shown in the image below.
Currently I have a page which displays the following information:
showID, showName, showAired, networkName, showStatus, showRuntime, showSeasons, showEpisodes, showOverview.
Ideally i'd like to have a List box to display an array of the genre's associated with the specific show. I have tried for quite a while to come up with a query to do this, the closest I managed to get showed the relevant information but added duplicate pages.
Here's my latest attempt:
SELECT * FROM Shows A
INNER JOIN Networks B ON B.networkID = A.networkID
INNER JOIN ShowGenres C ON C.showID = A.showID
INNER JOIN Genres D ON D.genreID = C.genreID;
Any help would be appreciated, thanks in advance.
One has to appreciate the presentation level logic that can be used in conjunction with the data logic. Very generically - in the 1:Many - - your query of fields that include both fields will repeat the 1 table values with each of the Many records. That can not be altered via any query design as it is inherent in the data logic.
But at the presentation level you can control the display. Using a report - the data source can be the query - but report properties offer grouping whereby you can put the 1 field value as the group header - thus displaying just once; and then below it list all the many records.
It is not a list box per se though depending on how creative one gets with reports/sub reports one could potentially make that style. But in the end you must work this at the presentation level.
One method is to use exists:
select g.*
from genres as g
where exists (select 1
from showgenres as sg inner join
shows as s
on sg.showID = s.showID
where sg.genreID = g.genreID and
s.showName = ?
);

SQL Query - SELECT WHERE Table1.ID = Table2.ID AND Table2.Var = #Var

I am trying to create an SQL query for an ASP NET drop down list using two tables and a session variable.
I want to retrieve the all values from table 1 which correspond to a matching ids in table 2, where table 2 is filtered by an external variable.
As it is clear I do not know how to word this question, here is a simplified example of what I am attempting to do:
My site has a session variable which holds the current colour the user is "filtering".
A dropdown list will show a list of cars corresponding to that colour using an SQL query.
For example if the session variable was "Blue" the dropdown list would contain "Punto" as it can see that the colour ID for "Blue" is 12 and "Punto" is the only car name corresponding to that colour.
Linked image: http://i.imgur.com/fe9L12c.png
As session variables can be assigned and called in ASP NET custom queries the session variable can just be referred to as, for example, #ExternalVar (Colours.ID WHERE (Colours.Name = #ExternalVar))
Apologies I had to word this like a quiz question; giving a simplified example was the only way I could really articulate my question.
I think this should do the trick if I understand the question above
select * from Cars c
inner join Colours cl on c.colourID = cl.ID
where cl.Name = #ExternalVar
SELECT Cars.* FROM Colours
INNER JOIN Cars
ON Colours.ID = Cars.ColourID
WHERE Colours.Name = #Variable
You can achieve this by using SQL Joins. Use below sql query:-
Declare #ExternalVar VARCHAR(50) = 'Blue'
Select C.Name FROM Cars C INNER JOIN Colours CO ON CO.ID = C.ColourId WHERE CO.Name = #ExternalVar

"Tag" searching/exclusion query design issue

Background: I'm working on a homebrew project for managing a collection of my own images, and have been trying to implement a tag-based search so I can easily sift through them.
Right now, I'm working with RedBean's tagging API for applying tags to each image's database entry, however I'm stuck on a specific detail of my implementation; currently, to allow search of tags where multiple tags will refine the search (when searching for "ABC XYZ", tagged image must have tags "ABC" and "XYZ"),
I'm having to handle some of the processing in the server-side language and not SQL, and then run an (optional) second query to verify that any returned images don't have a tag that has been explicitly excluded from results. (when searching for "ABC -XYZ", tagged image must have tag "ABC" and not "XYZ").
The problem here is that my current method requires that I run all results by my server-side code, and makes any attempts at sensible pagination/result offsets inaccurate.
My goal is to just grab the rows of the post table that contain the requested tags (and not contain any excluded tags) with one query, and still be able to use LIMIT/OFFSET arguments for my query to obtain reasonably paginated results.
Table schemas is as follows:
Table "post"
Columns:
id (PRIMARY KEY for post table)
(image metadata, not relevant to tag search)
Table "tag"
Columns:
id (PRIMARY KEY for tag table)
title (string of varying length - assume varchar(255))
Table "post_tag"
Columns:
id (PRIMARY KEY for post_tag table)
post_id (associated with column "post.id")
tag_id (associated with column "tag.id")
If possible, I'd like to also be able to have WHERE conditions specific to post table columns as well.
What should I be using for query structure? I've been playing with left joins but haven't been able to get the exact structure I need to solve this.
Here is the basic idea:
The LEFT OUTER JOIN is the set of posts that match tags you want to exclude. The final WHERE clause in the query makes sure that none of these posts match the entries in the first post table.
The INNER JOIN is the set of posts that match all of the tags. Note that the number two must match the number of unique tag names that you provide in the IN clause.
select p.*
from post p
left outer join (
select pt.post_id
from post_tag pt
inner join tag t on pt.tag_id = t.id
where t.title in ('UVW', 'XYZ')
) notag on p.id = notag.post_id
inner join (
select pt.post_id
from post_tag pt
inner join tag t on pt.tag_id = t.id
where t.title in ('ABC', 'DEF')
group by pt.post_id
having count(distinct t.title) = 2
) yestag on p.id = yestag.post_id
where notag.post_id is null
--add additional WHERE filters here as needed