I think a basic question, I know I have done similar in past. Looking for a quick pointer to get me running:
I have a large listing of [Contacts] (220K records) in a single table containing a field called [Contacts].[Title]. In a separate table called [Include] I have a collection of title substrings I'd like to act as a filtering criteria. Example: Title like *manager*, or like *director*, etc... from the [Include].[String] list.
Essentially (pseudo):
Select * from [Contacts] where [Contacts].[Title] is like *[Include].[String]*;
I am unclear how to JOIN these 2 tables or construct the SQL/Query.
Suggestions?
Follow-up, This produces the desired result
SELECT Contacts.Title
FROM Contacts
INNER JOIN Include ON Contacts.Title like '*' & Include.String & '*';
Related
Brief Explanation of problem:
I'm currently trying to create a report (RDL file) in visual studio that gets all the phone calls for a specific case, and prints them out in 2 tables that look like this: Current tables . My issue is that when I run the report, it is grabbing information from every case, instead of the particular case I'm currently in.
Here is the query I'm using to get the information for the tables:
SELECT
FilteredPhoneCall.regardingobjectidname,
FilteredPhoneCall.icms_contactname,
FilteredPhoneCall.owneridname,
FilteredPhoneCall.icms_callendtime,
FilteredPhoneCall.icms_informationgathered,
Filteredicms_impcase.icms_casenumber
FROM
FilteredPhoneCall,
Filteredicms_impcase
WHERE FilteredPhoneCall.regardingobjectidname
LIKE '%' + Filteredicms_impcase.icms_casenumber + '%'
How should I change my query so that it specifies that all information should be from a single case. Here are some pictures of the 2 tables I'm trying to join for clarity:
FilteredPhoneCall table and Filteredicms_impcase
Should I be using an inner join statement? Also let me clarify my thought process on why I'm using the LIKE statement is because of how the rows are in the system, the FilteredPhoneCall.regardingobjectidname is formatted like this: Name of company / CaseID, I need the 2nd half of that value, the CaseID in order to join the tables together to get the specific case value.
A)whether my syntax is working the way how I think it's working?
B)should I use a more specific identifier, like an INNER JOIN?
Don’t use “,” for joining 2 tables. It is worse than cross joins
Convert into Inner Join like below:
SELECT
FPC.regardingobjectidname,
FPC.icms_contactname,
FPC.owneridname,
FPC.icms_callendtime,
FPC.icms_informationgathered,
FC.icms_casenumber
FROM
FilteredPhoneCall FPC
INNER JOIN Filteredicms_impcase FC
ON FPC.regardingobjectid = FC.icms_casenumberid
WHERE FPC.regardingobjectidname
LIKE '%' + FC.icms_casenumber + '%'
I would transform the "regarding object id name" and perform the join explicitly:
SELECT
FilteredPhoneCall.regardingobjectidname,
FilteredPhoneCall.icms_contactname,
FilteredPhoneCall.owneridname,
FilteredPhoneCall.icms_callendtime,
FilteredPhoneCall.icms_informationgathered,
Filteredicms_impcase.icms_casenumber
FROM
FilteredPhoneCall
INNER JOIN Filteredicms_impcase
ON SUBSTRING_INDEX(FilteredPhoneCall.regardingobjectidname,'/',-1) =
Filteredicms_impcase.icms_casenumber
See this post for details: SQL SELECT everything after a certain character
I wonder if any can help me understand something I'm trying to solve.
I'm working on a wordpress site but this is more a sql question as I'm just querying to get some results within a template file.
I have a gallery of pictures which are advert boxes, and I need to pull these in relation to a supplied movie name, to do this Im using some custom fields on the ad pic called 'adlink' (link off ad) and ad
I'm using the nextgen gallery plugin and querying those tables, and I have three tables in total that contain the data I need to query.
ngg_pictures, nggcf_field_values & nggcf_fields.
the nggcf tables are custom fields tables,
I have got so far I can get what I need in two seperate queries, but I can't combine these into one query as it means querying the nggcf_field_values table twice, which I can't seem to sort.
I have hardcoded the search criteria in for the mo, but the 'close-encounters' bit would be a passed var, and the '156' would be the pid from the first query.
SELECT `eg_ngg_pictures`.`filename`, `eg_nggcf_field_values`.`fid`, `eg_nggcf_field_values`.`pid`
FROM eg_ngg_pictures, eg_nggcf_field_values
WHERE ((`eg_nggcf_field_values`.`field_value` LIKE 'close-encounters') AND (`eg_nggcf_field_values`.`pid` = eg_ngg_pictures.pid))
SELECT `eg_nggcf_field_values`.`field_value`
FROM eg_nggcf_field_values, eg_nggcf_fields
WHERE ((`eg_nggcf_fields`.`field_name` = 'adlink') AND (`eg_nggcf_fields`.`id` = eg_nggcf_field_values.fid) AND (`eg_nggcf_field_values`.`pid` = '156'))
any help would be greatly appreciated, I can get the results with what I have, but I like to understand how to combine these two and write better SQl. Thanks MRO
After looking at the Wordpress extension, I think the eg_nggcf_fields is the table that contains the name for a custom field. The eg_nggcf_field_values table contains the values of that custom field per picture.
So if you're looking for two fields called moviename and adlink, you have to look up two rows in the field_values table. You can join a table twice if you give it a different alias:
select pic.filename
, pic.pid
, fv1.field_value as MovieName
, fv2.field_value as Adlink
from eg_ngg_pictures pic
inner join -- Find ID for the field called 'moviename'
eg_nggcf_fields f1
on f1.field_name = 'moviename'
inner join -- Find value for field moviename for this picture
eg_nggcf_field_values as fv1
on fv1.pid = pic.pid
and fv1.fid = f1.fid
inner join -- Find ID for the field called 'adlink'
eg_nggcf_fields f2
on f2.field_name = 'adlink'
inner join -- Find value for field adlink for this picture
eg_nggcf_field_values as fv2
on fv2.pid = pic.pid
and fv2.fid = f2.fid
where fv1.field_value like 'close-encounters'
First of all, I'd recommend sticking to modern ANSI syntax for JOINing tables, which means using the JOIN clause.
Instead of using:
FROM table1, table2 WHERE table1.id = table2.pid
use:
FROM Table 1 JOIN table2 ON table1.id = table2.id
For simplicity's sake, I'd also recommend you to alias tables, as that tends to make the code more readable. Instead of having to write out egg_ngg_pictures every time, you can simply refer to the alias you assign it instead.
Lastly, when you use a LIKE operator, you usually add a wild-card character (typically %. I.e. LIKE '%123' or LIKE '123%'). You seem to look only for complete matches, which means you can just stick to using =, as that should give you slightly better performance.
Now to rewrite your query, I'd use something like the following:
SELECT
pic.filename
, fieldval.fid
, fieldval.pid
, fieldval.field_value
FROM
eg_ngg_pictures pic
JOIN eg_nggcf_field_values fieldval ON fieldval.pid = pic.pid
JOIN eg_nggcf_fields fields ON fields.id = fieldval.fid
WHERE
((fieldval.field_value = 'close-encounters')
AND fields.field_name = 'ad_link'
Note that I am not able to test the query, as I do not have your schema. But by incorporating the two queries into a single query, the join on the field_Values.PID retreieved with the 'close_encounters' value should already exist.
If the query does not work, feel free to create a SQL fiddle with the relevant tables and some data, and I'll try and get it to work with that.
I'm trying to query my database for unused listId's but can't quite seem to get the query syntax correct. Here is what I've tried:
SELECT DISTINCT pkListId FROM tblList
INNER JOIN InUseLists ON
tblList.pkListId NOT LIKE InUseLists.fkListId
Where tblList holds all List data, and InUseLists is a view holding all distinct listId's from my relational table.
Currently my tblList holds listId 1-9
my relational table has fkListId 1,8,9 (used more than once)
So my view gets distinct values from the relational table - 1,8,9.
What is wrong with my query syntax?
It sounds like you want a left join (which can succeed whether or not it finds a match in the "right" table), and then to select those rows where the join, in fact, failed to work.
Something like:
SELECT DISTINCT pkListId FROM tblList
LEFT JOIN InUseLists ON
tblList.pkListId LIKE InUseLists.fkListId
WHERE InUseLists.fkListId is null
(If LIKE is the right operator here. Most joins use a simple equality check)
If this still isn't right, it might be better if you edited your question to include the sample data and expected results in a tabular form. At the moment, I'm still not sure what the contents of your tables are, and what you're aiming for.
Try:
SELECT DISTINCT tblList.pkListId, FROM tblList, InUseLists WHERE
tblList.pkListId NOT IN(InUseLists.fkListId)
I have a table called faq. This table consists from fields faq_id,faq_subject.
I have another table called article which consists of article_id,ticket_id,a_body and which stores articles in a specific ticket. Naturally there is also a table "ticket" with fields ticket_id,ticket_number.
I want to retrieve a result table in format:
ticket_number,faq_id,faq_subject.
In order to do this I need to search for faq_id in the article.a_body field using %LIKE% statement.
My question is, how can I do this dynamically such that I return with SQL one result table, which is in format ticket_number,faq_id,faq_subject.
I tried multiple configurations of UNION ALL, LEFT JOIN, LEFT OUTER JOIN statements, but they all return either too many rows, or have different problems.
Is this even possible with MySQL, and is it possible to write an SQL statement which includes #variables and can take care of this?
First off, that kind of a design is problematic. You have certain data embedded within another column, which is going to cause logic as well as performance problems (since you can't index the a_body in such a way that it will help the JOIN). If this is a one-time thing then that's one issue, but otherwise you're going to have problems with this design.
Second, consider this example: You're searching for faq_id #123. You have an article that includes faq_id 4123. You're going to end up with a false match there. You can embed the faq_id values in the text with some sort of mark-up (for example, [faq_id:123]), but at that point you might as well be saving them off in another table as well.
The following query should work (I think that MySQL supports CAST, if not then you might need to adjust that).
SELECT
T.ticket_number,
F.faq_id,
F.faq_subject
FROM
Articles A
INNER JOIN FAQs F ON
A.a_body LIKE CONCAT('%', F.faq_id, '%')
INNER JOIN Tickets T ON
T.ticket_id = A.ticket_id
EDIT: Corrected to use CONCAT
SELECT DISTINCT t.ticket_number, f.faq_id, f.faq_subject
FROM faq.f
INNER JOIN article a ON (a.a_body RLIKE CONCAT('faq_id: ',faq_id))
INNER JOIN ticket t ON (t.ticket_id = a.ticket_id)
WHERE somecriteria
I have 2 SQL Tables:
attributesKey
id-------------name
C--------------Crunchy
S--------------Soft
R--------------Round
L--------------Long
produce
item---------attributes
Apple--------C,R
Orange------S,R
Bananna----L,S
Carrot-------L,C
This is a simplification of the issue I'm having; But, I think it should highlight the trouble.
Users enter attributes they are looking for (ie. Crunchy) and I should show produce who have the Key value "C" (ie. "Apple, Carrot").
~Pseudo Code for the SELECT statement ~
SELECT * FROM produce AS p WHERE (attributesKey.name = "Crunchy") AND (p.attributes LIKE attributesKey.id)
I hope that makes sense. I've been looking at it so long, its just become a set of random shapes on the screen.
Thanks
Try the below query -
SELECT * FROM produce p WHERE p.attributes LIKE '%(SELECT a.id from attributesKey a where a.name="Crunchy")%';
You can use FIND_IN_SET(str,strlist) like:
SELECT * FROM produce AS p WHERE FIND_IN_SET('C', attributes)
you can also use logical operators like AND or OR.
But a better solution would be to create a third able which represents the connection between the two. (The easiest way if you have numeric keys if your tables)
produce_attributes (produce_id, attributes_id)
and...
SELECT *
FROM produce AS p
LEFT JOIN produce_attributes AS pa ON pa.produce_id = p.id
LEFT JOIN attributesKey AS ak ON ak.id = pa.attributes_id
WHERE ak.name = 'Crunchy'
Edit:
Your attributesKey is called a dictinary table because it only resolves codes to values. And you have a many-to-many relationship between attributesKey and produce. Many-to-many relationships are represented by a connecting table in relational database management (or at least this is the most common representation of it).
So most of all I recommend you numeric keys and a third table.