MySQL with two Joins and a Where clause - mysql

I have 3 tables I want to join.
OFFICE contains address/contact details for an office.
CRIMECAT contains categories of crime law that an office may deal with and is related to the OFFICE table via the 'f_id'
CIVILCAT contains categories of civil law that an office may deal with and is related to the OFFICE table via the 'f_id' as well.
An office may deal with categories of crime law, civil law, both or none.
The user puts in a location and also decides via a range of checkboxes which areas of law they're interested in before hitting search. This should then return a list of the addresses of offices that deal with any of the checked categories in that location.
This works perfectly for either a crime category or a civil category, but as soon as one or more of each is selected the query returns zero results.
The working style of query is as follows:
SELECT DISTINCT OF.f_id, OF.acc, OF.add1, OF.add2, OF.add3, OF.city, OF.pc, OF.tel
FROM office OF
JOIN crimecat CR ON OF.f_id=CR.f_id
WHERE OF.id ='3946' AND CR.cat = 'crm'
The query I'm banging my head against a brick wall on is:
SELECT DISTINCT OF.f_id, OF.acc, OF.add1, OF.add2, OF.add3, OF.city, OF.pc, OF.tel
FROM office OF
JOIN crimecat CR ON OF.f_id=CR.f_id
JOIN civilcat CI ON OF.f_id=CI.f_id
WHERE OF.id ='3946' AND ((CR.cat = 'crm') OR (CI.cat = 'aap'))
I've also tried using a variant of the WHERE clause which also returns zero:
WHERE (OF.id ='3946' AND CR.cat = 'crm') OR (OF.id ='3946' AND CI.cat = 'aap')
I'm beginning to think the issue is with the JOIN(s) rather than the WHERE clause but can't think of a better way of writing them.

something like this may help. i suspect you are looking for either , but you are requesting both
SELECT DISTINCT OF.f_id, OF.acc, OF.add1, OF.add2, OF.add3, OF.city, OF.pc, OF.tel
FROM office OF
LEFT JOIN crimecat CR ON OF.f_id=CR.f_id
LEFT JOIN civilcat CI ON OF.f_id=CI.f_id
WHERE OF.id ='3946' AND ((CR.cat = 'crm') OR (CI.cat = 'aap'))

Related

Access Report from Query inexplicably multiplies columns

I have a weird problem. First off, I'm not well versed in Access, but I'm using it for a project to track visibility of log sources and detections and map them to the MITRE ATT&CK Framework. I tables such as Techniques to track the MITRE Techniques, Data_Sources to track the various sources used by those Techniques, and Data_Sources_Coverage that has a visibility ranking for each data source for the company. I have a query built with the below SQL (probably a little messy):
SELECT Technique.Name, Technique.ID, Technique.[Tactic ID], Count(Technique.[Data Sources].Value) AS Technique_Sources, Count(Data_Source_Coverage.[Data Source]) AS Sources_Visible
FROM (Data_Sources INNER JOIN Technique ON Data_Sources.Name = Technique.[Data Sources].Value) INNER JOIN Data_Source_Coverage ON Data_Sources.Name = Data_Source_Coverage.[Data Source]
WHERE (Data_Source_Coverage.Coverage)>0)
GROUP BY Technique.Name, Technique.ID, Technique.[Tactic ID];
The query works great, prints the results as I expect them. However, when I generate a report from that query things go off the rails. The report inexplicably (I did nothing to the data), multiplies the Technique_Sources column by the Sources_Visible column and displays that in the Technique_Sources Column, and then every entry in the Sources_Visible column becomes a '1'. Pics below. Thanks for the help.

SQL Is this Outer or Inner Join and how to join on Array

Seemed simple when I started and have done this before, now I confused myself and at a road block.
Have two tables: News_Table and a People_Table. Under the News_Table there is a field: News_People_Contributed and it has the ID's of the People_Table in array format (1,4,7,10) thus Four People contributed. I am creating a search parameter that looks up News_Header AND News_People_Contributed and can't figure how to create the search column.
News_Table
News_ID
News_Header
News_People_Contributed
People_Table
People_ID
People_First_Name...
Is it something like...
Select*
From News_Table
Left Join News_Table
On People_Table.People_ID IN (News_Table.News_People_Contributed)
Where Search_Param Like '%News_Header%' OR Search_Param Like '%People_First_Name%'
The problem is (News_Table.News_People_Contributed) is a string and the ID's are not. Plus I may not have people contributed etc. To make the issue even more complex, I'm doing this in MS Access instead of MySql, so have to code it "old school" sql for work around.
Perform a cross join and filter on matches in the string list. It says nothing about efficiency or form (as already commented on), but it works.
SELECT *
FROM News_Table, People_Table
WHERE InStr([News_People_Contributed],CStr([People_ID])) > 0;
This only answers part of the problem: The join -- the issue everyone seemed concerned about in the initial comments. There are not enough details about about the Search_Parameter to provide help on that. Supply more detail if you need more help there.

Access 'No Records Found' when Records Exist

I have a form in Access 2010 that's used as a search form to filter records matching specific criteria.
I transferred information in the backend from one set of tables to another. Now, the filter doesn't work. Even if I leave all the criteria blank - ie. set it to bring up all records - it tells me, 'No records found.'
I've remapped the tables a few times, made sure they all have information, and are linking and opening properly. What could be preventing Access from finding the records?
Here's the filter query, if it helps any. It doesn't appear to be filtering properly, even though it works fine with the old tables.
SELECT Activity.*, ActivityCash.*, EngSchDates.*, Monitoring.*, Procurement.*,
LookupDistrict.*
FROM ((((Activity LEFT JOIN LookupDistrict ON Activity.District =
LookupDistrict.District) INNER JOIN ActivityCash ON Activity.GWP = ActivityCash.GWP)
INNER JOIN EngSchDates ON Activity.GWP = EngSchDates.GWP)
INNER JOIN Procurement ON Activity.GWP = Procurement.GWP) INNER JOIN Monitoring ON
Activity.GWP = Monitoring.GWP ORDER BY Activity.District,
Activity.[ProgramYear], [Activity].GWP;
In general, to debug these types of problems, try removing one table at a time from the FROM clause (and SELECT) until you get your results back.
Remove AND [Activity].[Designer] like '*' from the query.

Need help on using SQL Query in Crystal Reports on Vb.NET 2010

I'm using MySQL with Vb.Net 2010 and Crystal Reports and to be honest I'm a complete noob when it comes to using Sql Queries. The problem is, I want to generate a report that will show the service fee from a transaction a person made while also showing the items bought, and their quantity and prices, basically a bill. I created a command from the data source I created in crystal reports and it contains this query:
SELECT t.transaction_code,t.fee,t.service_type,t.date_sched,total_bill, s.item_code, s.quantity_sold, i.item_description, i.price, CONCAT(p.fname,' ',p.mi,' ',p.lname),p.address FROM tbl_transaction AS t, tbl_sale AS s, tbl_item AS i, tbl_profile as p WHERE t.transaction_code = '{?trans_code}' AND s.transaction_code = t.transaction_code AND i.item_code = s.item_code AND p.id='{?p_id}'
It all works fine if I use that, the report shows everything like the persons name, transaction code, service type and its fee as well as all the items bought and the total bill. The problem is, if in that certain transaction, the said person only required services and did not buy any items, the report will just be blank. What I want is that the report should still be able to display the person's name, transaction code, service type and its fee, etc., even without buying an item. I know it's kind of hard to understand and it's also hard for me to explain, but I really need help right now.
Since I can't post images yet since I'm a new user. I'll just post a link to where I uploaded the structures of my tables:
http://oi42.tinypic.com/xogw9k.jpg
I also have a table called link_profile_transaction which does what it says and another table for the services which is linked to the services and fee from the transaction table(tbl_transaction) though it isn't included in the query.
http://oi41.tinypic.com/fqm2b.jpg
The links are legit and safe.
You must left join the items table to the service table.
Not really certain about the schema of your table, I am providing below for you to edit if it does not work as is.
SELECT t.transaction_code,t.fee,t.service_type,… s.item_code, s.quantity_sold,
i.item_description, i.price, CONCAT(p.fname,' ',p.mi,' ',p.lname),p.address
FROM tbl_transaction AS t, tbl_sale AS s, tbl_profile as p
LEFT JOIN tbl_item AS i ON i.item_code = s.item_code
WHERE t.transaction_code = '{?trans_code}'
AND s.transaction_code = t.transaction_code AND
p.id='{?p_id}'

MYSQL Selecting multiple values from the same column as a row

I have two tables
table COMMUNITY
id,
name,
etc.
table PROPERTY
id,
community_id,
type,
price,
rate,
etc.
There are four possible property types and a community can have info for one or more of the different types.
I am trying to figure out how to combine the information for each community into one row.
For instance I might want to get a row which includes
community.id, community.name, condo.price, condo.rate, town_home.price, town_home.rate
Of course there aren't tables for condo or town_home, the property type is represented in the property table by a column and a community can have multiple property rows.
My first thought was to use multiple joins on the table with aliases, but I can't seem to get anything that works properly.
Any suggestions?
You can use left join for this.
SELECT c.id, c.name, condo.price, condo.rate, town_home.price, town_home.rate
FROM Community c
LEFT JOIN Property condo ON condo.community_id = c.id AND condo.type = 'condo'
LEFT JOIN Property town_home ON town_home.community_id = c.id AND town_home.type = 'town_home'
I am the kind of person who doesn't like much to use joins, might be because I don't see much reason as for use them or as I wouldn't need it.
By any means, this is the way I would put it:
As you have a link between the two tables, you are able to use them to bring you the data you need, as long as you have this link you can bring whatever field you want.
So you said you wanted the fields:
community.id, community.name, condo.price, condo.rate, town_home.price, town_home.rate
SELECT
community.id,
community.name,
condo.price,
condo.rate
FROM
community, property condo
WHERE
community.id = condo.community_id;
This should solve it.
Try this:
SELECT * FROM community LEFT OUTER JOIN property ON community.id = property.community_id;
This should join the two tables on the ID field, so you should have one complete row with all of the information. if anything is null, it should be present in the row, as well. If multiple properties are listed with the same community ID, they should all come up in the result.
PS - I use PostgreSQL and not MYSQL so the syntax might be a bit different. Let me know how it works!