Overlap of unique Identifiers when trying to pull/expand data - mysql

LoadTableQuery.Qry = #"
SELECT `orders`.`ordID`,
`orders`.`OrderType`,
`prt`.`Name`,
`orders`.`prtID`,
`orders`.`ReqRef`,
`orders`.`DateOrdered`,
`orders`.`orderETA`,
`orders`.`DateRcvd`,
`orders`.`Status`,
`orders`.`ShipCost`,
`orders`.`Duties`,
`orders`.`Currency`,
`orders`.`Conv`,
`orders`.`Terms`,
`orders`.`Shipping`,
`orders`.`BuyerID`,
`orders`.`ShipTo`,
`orders`.`Notes2`,
`orders`.`Notes3`,
`orders`.`legacyID`,
`prt`.`Address`,
`prt`.`City`,
`prt`.`StateProv`,
`prt`.`PostalZip`,
`prt`.`Tel`,
`prt`.`Contact`,
`prt`.`Email`,
`prt`.`OrderTaxPc`
FROM `n2000`.`orders` left join `n2000`.`prt`
on (orders.prtID = prt.prtID)
prtID is a unique identifier which I'm using to pull the data I need about the party (their name, address, city..). The way it's written above works, accomplishing what I need it to do. getting and inserting the data into my table.
The issue is, I need a SECOND set of party data, based on orders.ShipTo instead of orders.prtID . ShipTo is really just a different prtID.
I'd like to be able to just run it again with
on (orders.ShipTo = prt.prtID).
I've tried using UNION, but that creates a new row, instead of adding columns to my current row. Which is where I'm looking to pull my data from.
The best way to understand it is, I've expanded prtID to add the Name, Address, City.. column data. I'd like to expand ShipTo as well. I don't know how to do that though without simply overwriting the prtID expansion.
The solution was actually quite simple. Hope this helps someone else
Solution

The solution was actually quite simple. Alias the different tables and do a double left join
Solution

Related

Variable.get() from Optionmenu not returning what I need to query mySQL

forgive me for shoddy coding/description of the problem. I'm new to programming and this is my first question!
Anyways, I am building a simple inventory system with tkinter while using mySQL as a database. Currently, I am working on a feature that would allow a user to pick a department using an Optionmenu and then get all the items in that department. I have the items listed in one table and the departments listed in another with a FOREIGN KEY connecting the items table to the primary key (department_string) in the departments table.
My goal is to have mySQL deliver a list of departments and then to have the Optionmenu use that list for it's options. I then need to query the database with the department selected in the Optionmenu to find all the items in that department. My problem is that variable.get() from the Optionmenu returns parentheses and commas that is first received when I query the database the first time. This makes it so I cannot directly input the variable.get() into the string in the cursor. Here is the code:
department_cursor.execute("SELECT department_string FROM departments")
department_list = department_cursor.fetchall()
variable = StringVar(search_window)
variable.set(department_list[0])
user_entry = OptionMenu(search_window, variable, *department_list)
***
cursor_b.execute("SELECT item WHERE department_string = " + "'" + str(variable.get()) + "'")
I believe the problem is that variable.get() provides the special characters like the parentheses and comma that came from the original mySQL query. For example if the departments are HR, Warehouse, R&D then mySQL returns [('HR',), ('Warehouse',), ('R&D',)] this is then fed into the Option menu which then variable.get() spits out something like ('HR',), and so mySQL doesn't recognize this.
So far the only things I can think of is to use a for loop to delete all the special characters in what the Optionmenu returns or to hard code what the string for each department should be. Both seem suboptimal and although I'm pretty new to programming I think it's a little too much like rube goldberg machine.
Anyways, if you made it this far, thank you so much for reading this! Once again, I'm brand new to all of this so any help you can give me is greatly appreciated!
.get() is going to return whatever is in the optionmenu. If the values you put into the optionmenu have the undesirable characters, the value you get out will too.
The database call is going to return a list (rows) of lists (columns), and it doesn't look like you're taking that into account.

Multiple databases, possibly creating a loop?

I have the following code below...the query works, but I'm looking for a better way to search though an entire column for a specific criteria. I think my question is going to require a loop, I'm just not sure how to perform it.
The last line of code states '
Where [dbIdwWhseLC].[dbo].[tbItemTxt].[sTxt] like '%258912.pdf
The value 258912.pdf is the value in
[IDEAUrlBot].[dbo].[IDEA Project Tracker].[Filename]
I would like to try and create a method where the query reads one value in sTxt, then compares the whole column to Filename. If it finds the value, then display sTxt, if not, go to the next value in sTxt and begin searching each value in Filename.
Please let me know if you need additional information. Thanks in advance.
Select [dbIdwWhseLC].[dbo].[tbItemTxt].[nItemId]
, [sTxtType]
, [IDEAUrlBot].[dbo].[tbl_IDWItems].[nUrlId]
, [IDEAUrlBot].[dbo].[tbl_Urls].[sUrl]
, [sTxt]
, [Filename]
, [dbIdwWhseLC].[dbo].[tbItemTxt].[vUpdateDt]
From [dbIdwWhseLC].[dbo].[tbItemTxt]
Left Join [IDEAUrlBot].[dbo].[tbl_IDWItems] on [dbIdwWhseLC].[dbo].[tbItemTxt].[nItemid] = [IDEAUrlBot].[dbo].[tbl_IDWItems].[nItemid]
Join [IDEAUrlBot].[dbo].[tbl_Urls] on [IDEAUrlBot].[dbo].[tbl_IDWItems].[nUrlId] = [IDEAUrlBot].[dbo].[tbl_Urls].[nUrlId]
Join [IDEAUrlBot].[dbo].[IDEA Project Tracker] on [IDEAUrlBot].[dbo].[tbl_IDWItems].[nUrlId] = [IDEAUrlBot].[dbo].[IDEA Project Tracker].[UrlId]
Where [dbIdwWhseLC].[dbo].[tbItemTxt].[sTxt] like '%258912.pdf'
If I understand you correctly, it ought to be possible to do this:
select itemTxt.[nItemId]
, [sTxtType]
, idwItems.[nUrlId]
, urls.[sUrl]
, [sTxt]
, [Filename]
, itemTxt.[vUpdateDt]
From [dbIdwWhseLC].[dbo].[tbItemTxt] as itemTxt
Left Join [IDEAUrlBot].[dbo].[tbl_IDWItems] as idwItems
on itemTxt.[nItemid] = idwitems.[nItemid]
Join [IDEAUrlBot].[dbo].[tbl_Urls] as urls
on idwItems.[nUrlId] = urls.[nUrlId]
Join [IDEAUrlBot].[dbo].[IDEA Project Tracker] projTracker
on itemText.[nUrlId] = projTracker.[UrlId]
Where itemTxt.[sTxt] like '%258912.pdf' -- not sure you intend this to remain
and projTracker.[FileName] = itemTxt.[sTxt]
But that's so simple that there must be some aspect to what you're looking for that's not clear to me.
Do you want to stop searching after you find a match between [FileName] and [sTxt]? If you want to return exactly one record, you can just change the first line to
select top 1 itemTxt.[nItemId]
... and add an ORDER BY clause to the end to control how the results are sorted and therefore which one is the "top 1".
Do you need to use wildcards when matching [FileName] and [sTxt]? It's not clear to me from the description which column would have the full path (or file name) and which would have just "258912.pdf", but you could change my last line to:
itemTxt.[sTxt] like ('%' + projTracker.[FileName])
If you need something more complex, like the first record from itemTxt.[sTxt] that matches projTracker.[FileName] for every record in projTracker, please say so in the comments.
If none of this is along the lines of what you need, you'll need to elaborate on what it is you do need. Please add more detail to your question, such as an example of what the output should look like or what you plan to do with it.

Append string to another table in MySQL

I’ve got a problem selecting some things in my database.
I’ve got 1 database with a lot of tables, however I only use 2 with this specific task.
I have a Clothing table, and I need to import this in my new database. I have succesfully transferred a lot of data, but now I need to take 1 final small step into completing this. In my Clothing table I have a column called Youtube link, here I have a link with an Youtube link, this specific link, in this specific column, I want to append that to another table I have in the database, let’s call it new_clothing_table. I there have a description column called prod_desc and I want to append the Youtube link to that column.
But there is also another “problem”, it’s not that every product has a Youtube link, so things have to be filtered in order to not fuck things royally up. A advantage I have, I have in both tables a product_name, and these are all the same. So I want to transfer that specific Youtube link if it’s there (sometimes there is a 0 filled in or nothing, but I dont think it’s NULL because if I make a SELECT query where Youtube_link is null I get zero rows..)
So can someone help me out>?
mysql has an update-join construct you can use:
UPDATE new_clothing_table nct
JOIN clothing c ON c.product_name = nct.product_name AND
c.youtube_link != '0'
SET nct.description = CONCAT(nct.description, c.youtube_link)
EDIT:
To make sure this does what you want, you could first select the updated content in order to examine it:
SELECT nct.description AS old_description,
CONCAT(nct.description, c.youtube_link) AS new_description
FROM new_clothing_table nct
JOIN clothing c ON c.product_name = nct.product_name AND
c.youtube_link != '0'

Crystal Report Self Joins

I'm trying to put a report together using Crystal Reports. I have to display a list of "class numbers" and the description of those classes. If there are prerequisites for those classes, I need to list those (the class number) as well as a description/title.
Currently I am able to display everything but the prerequisite descriptions. I believe I have to preform a self join since I'm only using one table. What I've done so far is use the command option when entering the tables I want to use for the report. In that command I have
SELECT Main.prerequisite, Sub.course_no, Sub.description
FROM course Main
JOIN course Sub ON Main.prerequisite = Sub.course_no
where main.prerequisite is not null
The table is called "course" while the columns are "course_no", "description", "prerequisite".
Anytime I add "description" to the report, it only gives me the course_no's corresponding description, not the prerequisite description.
I'm not sure what I'm doing wrong or what I'm not doing at all, but any help would be greatly appreciated. Thank you.
I figured a work around and thought I should post it here. Hopefully to avoid wasting anyone's time as well as providing my solution to anyone that may have a similar issue.
I simply created a view in SQL
Create view preReqs AS
SELECT Main.course_no AS [course_no]
,Main.description AS [description]
,Main.prerequisite AS [prerequisite]
,Sub.description AS [preReqDescription]
FROM course As Main
LEFT OUTER JOIN course As Sub ON Main.prerequisite = Sub.course_no
After I created that, I just imported the view instead of the table itself into the Crystal Report. I was then able to group by whatever I wished.
Hopefully this will help anyone else that may run into this issue. Thank you for everyone that provided their time/input as well. I appreciate it.

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!