Error in SQL SELECT with INNER JOIN - sql

So, basically, I have two tables called "dadoscatalogo" and "palavras_chave", with a common field, "patrimonio" which is the primary key of "dadoscatalogo".
I'm using a servlet to connect to the database with these tables, and passing a query to search for entries based on some search criteria that's defined by the user.
Now, since the user can search for entries based on information present in both tables, I need to do an INNER JOIN, and then use WHERE to search for that info. I'm also using LIKE, because the user may pass just part of the information, and not all of it.
So, to test it all out, I tried passing it a few parameters to work with, and see how it went. After some debugging, I found out that there was some mistake in the query. But I can't seem to be able to point out exactly what it is.
Here's the test query:
SELECT dadoscatalogo.patrimonio
FROM dadoscatalogo
INNER JOIN palavras_chave
ON dadoscatalogo.patrimonio=palavras_chave.patrimonio
WHERE dadoscatalogo.patrimonio LIKE '%'
AND dadoscatalogo.titulo LIKE '%tons%'
OR palavras_chave.palchave LIKE '%programming%';
So, basically, what I'm trying to do with this query is, get all the primary keys from "dadoscatalogo" that are linked to a record with a "titulo" containing "tons", or a "palchave" containing "programming".
PS. Sorry for the names not being in English, hopefully it won't be too much of a distraction.
EDIT: Right now, the tables don't have much:
This is the dadoscatalogo table:
http://gyazo.com/fdc848da7496cea4ea2bcb6fbe81cb25
And this is the palavras_chave table:
http://gyazo.com/6bb82f844caebe819f380e515b1f504e
When they join, I'm expecting it to have 4 records, and it would get the one with patrimonio=2 in dadoscatalogo (which has "tons" in titulo), and the one with palchave=programming (which would have patrimonio=1)

As per my understanding run below query:
SELECT dadoscatalogo.patrimonio
FROM dadoscatalogo
INNER JOIN palavras_chave
ON dadoscatalogo.patrimonio=palavras_chave.patrimonio
WHERE dadoscatalogo.titulo LIKE '%tons%'
OR palavras_chave.palchave LIKE '%programming%';

Related

Using MS Access to obtain data across linked tables

I'm new to MS Access and am trying to speed up a data gathering process that is taking forever in Powershell. In Powershell I have 10 or so web API calls to get data and each comes back as an object with multiple properties (fields.) Each set of data has related fields to 1 or more of the other sets of data. Getting the data is very quick but piping an array of objects to where-object to select-object takes over an hour and there's really not that much data. Each object contains 500-1500 "records" and 5 to 10 "fields" so I thought why not export that data and use something that's intended to search through data to do the job? I exported each object as a separate .CSV file. So enter MS Access..
I imported each of the CSV's as a separate table (easy enough.) I'm going to simplify this down for this example to the following 3 tables:
[Tables]https://i.stack.imgur.com/UCH1F.jpg
Every table has fields that relate it over to other tables. Pretty much there's some sort of Id field in every table that is related to another Id field in a different table that I need to pull a field called "name" from. I'm trying to follow the bread crumbs from the Player name to it's Network name to it's Application name, to it's Layout name, etc... I want to build a query that I would eventually just be able to export as an Excel file. I also would prefer to just write out the SQL unless it's really easier to to understand the visual query builder. I'm looking to build a sheet with the following information:
Player's Name would include all names from the Players table and getting just that data makes sense to me. SELECT Name AS PlayerName FROM Players Everything else, not so much. I feel like this will end up being some mega query as I get deeper into related table after related table. In Excel, it would be straightforward using Vlookups across tabs but that doesn't seem to be the best approach. Given the info above, I'm trying to achieve the following output:
Result table
Any help with strategy and syntax greatly appreciated!
You're looking for the JOIN clause.
SELECT
Players.Name PlayerName, Networks.Name PlayerNetwork, Applications.Name ApplicationName
FROM
Players
LEFT OUTER JOIN
Networks
ON
Networks.ID = Players.NetworkId
LEFT OUTER JOIN
Applications
ON
Applications.Id = Players.ApplicationID

Adding New Fields via LEFT JOIN in Running ETL from SQL to MongoDB

I need to run an ETL to get data from a Sybase/SQL back-end into my MongoDB environment. We already have data from Sybase, but now there are a couple of additional fields we want to pull data in for. So with my familiarity being with Mongo (not so much Sybase), I'm trying to determine how I need to adjust our ETL to get this additional data.
The current SELECT statement looks like this:
`SELECT DISTINCT TOP 100 d.*, d10.code code10, d10.id_number as Code10ID FROM diagnosis d LEFT JOIN diagnosis_icd10 d10 on d.icd10_id = d10.id_number ORDER BY d.id_number`
Now, within the diagnosis_icd10 table that we're doing the LEFT JOIN on, there are now a couple of extra fields available.
So, my question is, do I need to explicitly include these additional fields here in the SELECT statement in order for them to be available in the ETL process? Or is this only the case if I want to rename the fields? What should this look like?
Yes you need to explicitly request them, as your current query only gets all columns from the diagnosis table (aliased to d via the d*).
This has nothing to do with Sybase though this is basic SQL so would be the same for most databases. You just add them to the select statement as in d10.column_name like the others.

Oracle Application Express SQL Query to show meaningful information

I am trying to write a query that 1) works and 2) shows meaningful information.
However, I can't seem to complete both scenarios. Both bits of code do work to a degree. My SQL query does work by showing all the useful information a user wants but when you click the edit button it doesn't link properly so it won't allow the user to update that row. The other shows only keys and rowid but when you click edit does show the information and allows it to be updated.
So as not to get another down-voted question, I have taken pictures of each scenario to show the problem, but, ultimately, I need to show meaningful information: an id or key isn't meaningful to the vast majority of users.
Here is my code
SELECT APPLICATIONS.APP_ID, APPLICATIONS.SRN, STUDENTS.SURNAME, STUDENTS.FORENAME, APP_STATUS.STATUS, METHODS.METHOD, JOBS.JOB_TITLE, APPLICATIONS.APP_DATE
FROM APPLICATIONS
JOIN STUDENTS
ON APPLICATIONS.SRN = STUDENTS.SRN
JOIN APP_STATUS
ON APPLICATIONS.STATUS_ID = APP_STATUS.STATUS_ID
JOIN METHODS
ON APPLICATIONS.METHOD_ID = METHODS.METHOD_ID
JOIN JOBS
ON APPLICATIONS.JOB_ID = JOBS.JOB_ID;
and here are the pictures of it in action
below is the code that does not show meaningful information but does work.
select "ROWID",
"APP_ID",
"SRN",
"STATUS_ID",
"METHOD_ID",
"JOB_ID",
"APP_DATE"
from "#OWNER#"."APPLICATIONS"
If i knew how to properly use rowid i am sure this is a simple feat but i dont so if i could get any help it would be useful
//edit
who ever renamed this to Application Expression why? what i am using is Apex Application Express it was relevant information that got changed to something wrong which might make it hard for someone with a similar problem to find later.
In the second, simple query, apex can determine which table (and record) you are trying to edit.
In the first query, with the joins, it can't tell which of the five tables in query you want to edit. You probably want to have the edit link pass the primary key of the row from APPLICATIONS to the child page. You would need to build into that page any logic (lists of values etc) that map lookup tables (such as status) to the values needed in the APPLICATIONS table.

Search through Users with dynamic attributes with SQL

.Hi i'm working with Asp and SQL-Server and i have no problem with writing dynamic query
I'm trying to Write a search page for searching people.
I have 3 related tables:
See my table diagram in : http://tinypic.com/r/21159go/5
What i'm trying to do is to design a search page that a person can search users with a dynamic number of attributes.
Example:
think that a username called "User1" has 3 attributes named "Attr1", "Attr2" and "Attr3" related to him in "UserAttributes" table and "User2" has 3 attributes named "Attr1", "Attr2" and "Attr4".
Attribute names and other bunch of items unrelated to search function saved in "Attributes" Table. This is because i want to relate an attribute between multiple users. and their values are stored in "UserAttributes" table.
Well someone wants to search upon "Attr1" and "Attr2" and wants to return all users that have "Attr1" and "Attr2" with specific value.
I need a query to know how to implement this. I can write a dynamic query with asp.net so if someone please give me a query for this one example i have brought, i would be thankful
P.S. This is not my real database. my real database is much more complex and has more fields and tables but i just cut it and brought only necessary items. and because attributes are very dynamic they can't be embedded in table columns.
Thanks in advance
Based on your DB diagram your code would be something like this
update:
SELECT u.*
FROM users AS u
LEFT OUTER JOIN UserAttributes AS ua1
ON u.USER_ID = ua1.USER_ID
LEFT OUTER JOIN UserAttributes AS ua2
ON u.USER_ID = ua2.USER_ID
WHERE (
ua1.attribute_id = 'att1'
AND ua.attribute_value = 'MyValue' )
AND (
ua2.attribute_id = 'att2'
AND ua.attribute_value = 'MyValue2' )
In where clause you would specify the attirbute_Id and what value you are expecting out of it. Than just decide if you want to restrict users to have all values match or just one of them, in that case modify AND between statements to be OR
if you just want to do this quick and dirty you can create your own class library that can create adhoc sql that will pass to the database.
if you want more organized matter, create SP that will bring back users and accepts any left of id and value. Do a lot of that by passing list separated by comma, colon or semicolon. Than split it up in SP and filter results based on those values
there are many other alternatives like EntityFramework, LINQ-to-SQL and other options, just need to figure out what works best for you, how much time you want to spend on it and how easy will it be to support later.

sql update multiple rows with multi join subselect

This is an updated part 2 of a question I asked earlier. I'm trying to make the following update, but this query does not actually seem to do anything.
UPDATE u
SET graduation_class_id = gc.graduation_class_id
FROM [user] u
JOIN graduation_class gc
ON u.graduation_class_id = gc.graduation_class_id
JOIN graduation_term gt
ON gt.graduation_year_id = gc.graduation_year_id
TABLE SCHEMA
**user
user_id
graduation_class_id
**graduation_class
graduation_class_id
graduation_year_id
**graduation_term
graduation_term_id
graduation_year_id
The goal is to get the matching graduation_class_id value into the user table. I've been told this won't work because the match won't be found unless the user already has the matching graduation_class_id. That is a problem, because I'm trying to actually get the proper one in there!
This idea is fundamentally doomed to fail. You're trying to get the SQL server to link a graduation class to a user by itself, despite the SQL server not having information on which graduation class should be linked to the user. Unless you have some data somewhere (in user, in graduation_class, in some other table) that links a user_id to the graduation term, class, or year (or someone manually telling SQL which data match up), SQL can't magically do that job for you.