So, I have myself all confused with how to pull info from three tables. Let me set this up fairly clearly (I hope)...
I have three tables FoodTruck, MenuItems, and Ingredients. Each food truck can have multiple menuItems (has a truckID column tied to the FoodTruck truckID) and each menuItem can have multiple ingredients (menuID column tied to the MenuItem menuID).
So, I am thoroughly confused on the mysql command to get the information I truly want (joins are still fairly confusing to me) because in the end I would LIKE to be able to show a user the Food Truck's menu. It would look like one big menu consisting of menuItems which include ingredients.
If there is a select query that would get all of that information, what would the output be comprised of? I mean, how would it be presented? An array of the menuItems and then an array of the ingredients? If I got the info I could figure out how to deal with it as I am using JDBC...
Forgot to add: in the Menu table there are the columns (primary key: menuID, foodName, price, foodType, specialComments)... I need all of those. In the Ingredients table, I have name and menuID but I really only need to display name.
I have most of the queries done that I need. It is just that I am trying to display the full menu now. I am using the DAO design and prepared statements to sanitize the input. In doing that, I was hoping to be able to use a join or somehow get all of that pertinent info without resorting to several queries to my database
Yes you can do it with one query, using LEFT JOIN.
SELECT
* // I used *, you should modify it if you want to better control the output
FROM FoodTruck ft
LEFT JOIN MenuItems mi
ON mi.truckID = ft.truckID
LEFT JOIN Indgredient ind
ON ind.menuID = mi.menuID
Left join will keep records from the table on the left even no rows can be found in the table on the right.
Take note that rows from FoodTruck are likely to duplicate because you have more than one menu items for each truck. The same will also happend to MenuItem.
Related
So i a bit lost and don t really know how to hang up this one...
Consider that i have a 2 DB table in Talend, let say firstly
A table invoices_only which has as fields, the invoiceNummer and the authors like this
Then, a table invoices_table with the field (invoiceNummer, article, quantity and price) and for one invoice, I can have many articles, for example
and through a tmap want to obtain a table invoice_table_result, with new columns, one for the article position, an one other for the total price. for the position i know that i can use something like the Numeric.sequence("s1",1,1) function, but don t know how to restart my counter when a new invoices nummer is found, and of course for the total price it is just a basic multiplication
so my result should be some thing like this
Here is a draft of my talend job, i m doing a lookup on the invoicenummer between the table invoice_only and invoices
Any Advices? thanks.
A trick I use is to do the sequence like this:
Numeric.sequence("s" + row.InvoiceNummer, 1, 1)
This way, the sequence gets incremented while you're still on the same InvoiceNummer, and a new one is started whenever a new InvoiceNummer is found.
There are two ways to achieve it,
tJavaFlex
Sql
tJavaFlex
You can compare current data with the previous data and reset the sequence value using below function,
if () {
Numeric.resetSequence(seqName, startValue);
}
Sql
Once data is loaded into the tables, create a post job and use an update query to update the records. You have to select the records and take the rank of the values. On top of the select you have to perform the update.
select invoicenumber, row_number() over(partition by invoicenumber, order by invoicenumber) from table name where -- conditions if any.
Update statements vary with respect to the database, please provide which database are you using, so that can provide the update query.
I would recommend you to achieve this through Sql
I am trying to create a multi-layer menu with no restriction on how far it can branch out - e.g. Food->Fruits->Round Shape->color etc...
I am not sure how to represent the relationship in SQL and also am not sure how to query the data.
I thought about doing:
MainMenu:
ID:ChildID:Name:URL:PermissionID:etc.
SubMenu:
PID:ChildID:Name:URL:PermissionID:etc.
I think if the submenu has additional submenus, its children would be another item in the SubMenu table, and that should work? The PermissionID is used to determine if the Menu item can be accessed by a UserGroup.
I am not sure about the SQL query since i am a little green on it. I know in the end I am feeding UserGroup's PermissionIDs to the SQL to get all the Menu and Submenu from the tables.
Storing hierachical data in RDBMS is not a good idea. Either you can use document based frameworks like Elastic https://www.elastic.co/ which stores data in JSON document format or you can use hierachical JSON in varchar column in RDBMS.
Try to get the best practice of handling count of how many times books being "i like"d. Let's say right now, I have many series of books and each series can have many books. Now, when people click "i like" to one book, I want to add 1 to the book and also add 1 to the series so that later I can render these numbers, correspondingly. Now, I am struggle on two ways.
1) when one book is clicked, I will add 1 on the "liked" column in the book table and at the same time, add 1 on the "liked" column in the series table.
2) when one book is clicked, I only add 1 on the "liked" column in the book table. When I try to do a render of the number for the series, I do a SUM on the "liked" column of the books that belong to the series.
However, both of have pro and cons. The 1) way, I can simply fetch the "liked" column of the series table when I try to show how many people liked the whole series. It will be efficient than the 2) way cause there is no aggregation needed, especially when many people try to render the series page. However, this will take more effort when we click "i like" button. Cause even when people click on different books. As long as these books belong to the same series, it will need to update the number of the series. And it will be concurrent process. On the contrary, if I don't do update on the series table when people click "i like" for the book. It will be more efficient at that level, but will waste a lot of effort to do the redundant aggregation calculation when many people try to load the same series page, where the "liked" number for the series is shown.
Any other ideas? If no, what is a better solution? 1) or 2)? Thanks in advance.
I prefer the second approach. The logic there makes more sense. Instead of just adding 1 to the series each time the book is clicked, you can just sum up the total number of likes on the series.
Store your data in Database, otherwise will lost data when server restarts.
I recommend using a database. You can have a books table and a likes table. When someone likes a book, you take the id of the book they liked and add it to the likes table.
This way you can also store the name of the person who liked it, and stop them from liking more than twice, and you can also give them the option to remove their like.
For example:
Books Table:
id, bookname, author, (and any other information)
Likes Table:
id, username, book_id, date
Then when counting how many likes there are for a single book, you can query the database and count the number of rows for where the book_id = ?
Using a RDBMS, you should focus on data integrity. The first approach can introduce an update anomaly, if not done in a single transaction. Anyway, you would have high concurrency on the series counter. So I would prefer the second approach.
Start with a denormalization only if your solution lacks of performance.
Use HttpSession
HttpSession session = request.getSession(false);
if (session == null) {
// store value in session
}
else {
// read value from session
}
Another option is to use Redux for larger Single Page Applications but only if makes sense.
Simple question really. This is using JPA on Java and what I what to do is to have a table with and column which can refer to one of two tables. To make this clearer I can have a 'User' table and a 'TempPerson' table. I don't want to pollute my User table (as I use it for security as well, plus has other info as well). Now lets say I have a third table called 'Game'. Now when someone stars a game against someone, they can play against someone in the system already ie. User or someone where they can type a name and new entry for TempPerson is created and used. So the game for player2 (or player1) will be a mapped id to either User.id or TempPerson.id. Now I understand that a determining column may need to be placed into Game to determine what the Id is for but I hope JPA will cater for it somehow. Any ideas will be helpful, i could use inheritance but not sure about it.
Here is another example:
Lets say I have a table which holds information about images => id, resolution, width, height, location, bucket .... id_in_the_table_where_used, table_name_of_where_used. Now, this one table can hold the images for profiles, places, etc... and the profiles, places will have an id referring to the images table, but I also would like the images table to have an id back to where the images is used, which table and which id is using it.
It almost I am asked i 'one to many tables' solution. Although I could have many in between tables etc... Seems overkill to so something quite simple, although many DBAs may be cursing this idea. It does minimise queries, number of tables etc...
Thanks in advance
It is possible to use single FK to target multiple tables. You would have to use #JoinColumn for that
#Entity
public class User{
#OneToOne
#JoinColumn("universalId", targetEntity=Avatar.class)
private Avatar
#oneToMany
#JoinColumn("universalId", targetEntity=Log.class)
private List<Log> logs;
}
This would use universalId column of User's table to lookup related records from Avatar and Log tables
This however is rather anti-pattern, causing a lot of consequences when for example universalId will have to be changed etc. 1 column = 1 FK - go that way.
I am authoring a javabean and would like to know if it should include properties to set values obtained from a sql join?
Say I have database tables for Products and Orders. These are also my java bean names.
Now I want to display a list of all products but in addition to all the product properties, I want to have columns to display last purchase date and last purchased by
My db query to get the product list would need to do joins to gather the additional info. It does not seem correct to have setters and getters for `last purchase date and last purchased by' in Product.java model. I may want to have a 3rd column so constantly adding new columns to my bean doesn't make sense.
How do you go about this? I seem to encounter this when needing to display lists of models in my view.
Have a Map in Product. Upon firing the sql, store hte results of the join as key value pair in the Map. K=lastpurchasedby(columnName) and V= the value
So no need to add individual attributes. They all come in key-value pairs.