|
I want to store some data like this:code:
The problem with this is that I can't figure out how to pull out the information again without joining the table to itself for each layer of children, which means I'd have to limit the number of possible tiers... I guess that wouldn't be the end of the world but it feels messy. Anyone know a better way?
|
# ? Feb 16, 2010 20:38 |
|
|
# ? May 15, 2024 23:26 |
|
Hierarchical data retrieval is not one of the strong points of a relational database. It's a longstanding problem that everyone has. There is no easy solution that will provide super duper easy access to the data.
|
# ? Feb 16, 2010 20:42 |
|
Vince McMahon posted:I want to store some hierarchical data http://dev.mysql.com/tech-resources/articles/hierarchical-data.html
|
# ? Feb 16, 2010 20:42 |
|
Is there any variable/size significance to Dec 31 2069 (2069-12-31 00:00:00.000)? I'm getting some weird imports from other systems that are using this date and I need to figure out if it's happening because of bad data types on their side or bad conversion on mine. Most of the time appropriate dates show up in my system (datetime fields) but occasionally a 2069 will sneak in there, when the data from the originating system when I query it is 'clean'. EDIT-never mind, found. Largest possible 2-digit datetime value (here we come y2k70!). I knew it seemed familiar. I think he's getting cute with data types on the originating system's end. Scaramouche fucked around with this message at 22:03 on Feb 16, 2010 |
# ? Feb 16, 2010 22:00 |
|
Triple Tech posted:Hierarchical data retrieval is not one of the strong points of a relational database. It's a longstanding problem that everyone has. There is no easy solution that will provide super duper easy access to the data. I'd say that's more MySQL. I've had to do hierarchical data in Oracle and used CONNECT BY. PostgreSQL also added WITH RECURSIVE that lets you do similar things, and I think that one is somewhat ANSI-standard. http://www.postgresql.org/docs/8.4/static/queries-with.html
|
# ? Feb 17, 2010 02:20 |
|
Here's a comparison of four popular ways to model hierarchical data.
Bad Titty Puker fucked around with this message at 06:37 on Feb 17, 2010 |
# ? Feb 17, 2010 06:34 |
|
I'm trying to do a multi-table delete with MySQL, here is my current SQL statementcode:
|
# ? Feb 17, 2010 23:50 |
|
VerySolidSnake posted:Is there a better way to go about this? Just write separate delete statements. Multi-table delete is sort of a crazy construction, and doesn't really buy you anything. Why do you think you need it? Anyway, to directly answer your question, I think you need to have delete t1,t2 from t1,t2... as your syntax, based on your query I strongly suspect you should be using full outer joins instead of inner joins (on a second glance, you probably only need left/right outer join in code you have, but that assumes you have foreign keys setup right and don't have any dangling references), and it looks like you may have foreign keys in your table structure which will randomly cause that statement to fail because MySQL doesn't do the underlying deletes in any particular order. ShoulderDaemon fucked around with this message at 01:00 on Feb 18, 2010 |
# ? Feb 18, 2010 00:55 |
|
VerySolidSnake posted:I'm trying to do a multi-table delete with MySQL, here is my current SQL statement i'm going to assume papers it the main table? code:
|
# ? Feb 18, 2010 00:57 |
|
Sprawl posted:i'm going to assume papers it the main table? Note that this will delete zero rows if, for example, a paper has no keywords (because the inner join will find no rows at all). You can convert it to left outer joins in this case, but then it will fail if you are trying to delete keywords for a paper that no longer exists. You can prevent that degenerate case by creating foreign keys to prevent dangling references, but then it will randomly fail because multi-table delete doesn't delete from the tables in any particular order, so it may choose to delete frm the papers table first, break the foreign keys, and abort the transaction.
|
# ? Feb 18, 2010 01:02 |
|
ShoulderDaemon posted:Note that this will delete zero rows if, for example, a paper has no keywords (because the inner join will find no rows at all). You can convert it to left outer joins in this case, but then it will fail if you are trying to delete keywords for a paper that no longer exists. You can prevent that degenerate case by creating foreign keys to prevent dangling references, but then it will randomly fail because multi-table delete doesn't delete from the tables in any particular order, so it may choose to delete frm the papers table first, break the foreign keys, and abort the transaction. Yes there are a few issues with it i normally would never use such a method and stick to a transaction with 1 table at a time myself so you can rollback if something like an error happens.
|
# ? Feb 18, 2010 01:06 |
|
Hi, I've never posted in here before, I've worked with Sql for just under a year now. Today, what I effectively had to do was select from a single table of around two thousand records two rows, selecting them by the guid primary key of the table. The table has a sort order, so one row has priority over the other. If any of the values in the first row are null, use the value in the second row, if not, the value in the second row is irrelevant. The end goal is to return one row. The purpose of this is to use it in an update statement on the same table, using it to update the first row to the returned row. I eventually hacked my way to solving it using select cases and subselects, but it was really poorly done, and it really felt like the kind of thing that in the hands of someone with a more organised mind would be fairly easy and quicker. Does anyone have any suggestions?
|
# ? Feb 18, 2010 01:31 |
|
Primpod posted:Hi, I've never posted in here before, I've worked with Sql for just under a year now. Well i assume they have a common data point right? code:
If this can't work for you maybe you could describe your data or what sql engine your working with etc etc a little more. Sprawl fucked around with this message at 02:27 on Feb 18, 2010 |
# ? Feb 18, 2010 02:17 |
|
Thanks for the help!
|
# ? Feb 18, 2010 14:23 |
|
So I've got multiple tables and 2 have values I'm trying to sum. I've been trying to figure out how to get this in one query for about 4 hours and I'm only going in circles. A bunch of tables are involved, and its too difficult to explain completely, but essentially what I'm running into is double/triple/etc counting for records that appear in certain tables that many times. The tables look like so: code:
Corsair Jr fucked around with this message at 01:28 on Feb 23, 2010 |
# ? Feb 23, 2010 01:24 |
|
Corsair Jr posted:
Are the columns the same names/datatypes in values1 and values2? If so, you can UNION those tables together, and then join that back onto your programs table.
|
# ? Feb 23, 2010 01:29 |
|
slartibartfast posted:Are the columns the same names/datatypes in values1 and values2? code:
Again, I'm grossly oversimplifying this. The code has a shitload of conditional statements, aggregates, and intersection tables that are make it look messy as hell. [edit]Actually I'm a moron and I think just grouping at the end of the UNION will fix the sums. Thanks guys. Corsair Jr fucked around with this message at 01:45 on Feb 23, 2010 |
# ? Feb 23, 2010 01:35 |
|
Vince McMahon posted:I want to store some data like this: Read up on the modified preorder tree storage method in the link below. It's very useful and easy for storing and retrieving hierarchical data. It works beautifully for huge datasets, too. butch thumperton posted:http://dev.mysql.com/tech-resources/articles/hierarchical-data.html
|
# ? Feb 23, 2010 04:06 |
|
I have a table like this:code:
code:
|
# ? Feb 23, 2010 17:23 |
|
Sub Par posted:I'm having a brain freeze and can't think of a particularly easy way to do this (Oracle 10g). Can someone help? Thanks. It's a pain in 10g. An article linked earlier in the thread does the best job of summarizing the task in Oracle environments. You will find that many of the solutions do not speak to the related problem of ordering, however. http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php I do this infrequently and use specific functions to solve the problem.
|
# ? Feb 23, 2010 17:41 |
|
var1ety posted:It's a pain in 10g. An article linked earlier in the thread does the best job of summarizing the task in Oracle environments. You will find that many of the solutions do not speak to the related problem of ordering, however. Thanks. I may end up doing this outside the database, as I can think of a million ways to that. I'll check out that link. Edit: my vendor is waiting for 11g release 2 to come out on for Windows before upgrading. Anyone in the know have an idea when that might be? Edit again: WM_CONCAT() to the rescue! Thanks again, that worked like a charm. Sub Par fucked around with this message at 17:47 on Feb 23, 2010 |
# ? Feb 23, 2010 17:42 |
I'm trying to use phpMyAdmin with a database user that doesn't have SHOW DATABASES permissions. As a result, I can't select one. I've tried using "use <database name>", which doesn't return any errors, but I still can't manipulate the database through the interface, other than using the SQL window. Is there any way I can configure phpMyAdmin to use a specific database? Or another tool I should use instead? edit: resolved (kinda). Ended up using SQLyog instead. Not an ideal solution, since it's a Windows install, but it got the job done. Tinestram fucked around with this message at 10:44 on Feb 27, 2010 |
|
# ? Feb 26, 2010 23:51 |
|
I have a backup script that looks like this:code:
code:
code:
|
# ? Mar 5, 2010 20:48 |
|
You can pass multiple --table arguments to pg_dump, so your backups won't be inconsistent.
|
# ? Mar 5, 2010 22:43 |
|
ShoulderDaemon posted:You can pass multiple --table arguments to pg_dump, so your backups won't be inconsistent. Ok, now I have this: code:
code:
|
# ? Mar 6, 2010 02:26 |
|
You can combine your pg_restore commands into a single command as well, which you should do to preserve integrity by keeping everything in a single transaction. If you pass --disable-triggers to pg_restore then it will prevent foreign key checks from being applied to the data as it restores (or, rather, defer them to the end of the transaction) and allow your data to be restored in any order.
|
# ? Mar 6, 2010 04:05 |
|
Easy question: If I have a query that returns some rows thus:code:
code:
Is this possible in MySQL (innoDB) or do I need to use PHP to do the looping? Sorry if this is trivial, but I've always just assumed I needed to loop before. EDIT: I know I can do this with a JOIN, the trouble is, the F3 is then looked up in another table. EDIT: 2... I now have the finished query using JOINS. If there's a way of doing it without JOINs, I'd be happy to know. Thanks. Fruit Smoothies fucked around with this message at 02:22 on Mar 7, 2010 |
# ? Mar 7, 2010 01:20 |
|
ShoulderDaemon posted:If you pass --disable-triggers to pg_restore then it will prevent foreign key checks from being applied to the data as it restores ah thanks that did it
|
# ? Mar 7, 2010 02:10 |
|
Why are you avoiding joins?
|
# ? Mar 7, 2010 04:51 |
|
Fruit Smoothies posted:Is this possible in MySQL (innoDB) or do I need to use PHP to do the looping? edit: search for "pivot table" or "crosstab" and MySQL Bad Titty Puker fucked around with this message at 05:58 on Mar 7, 2010 |
# ? Mar 7, 2010 05:12 |
|
Triple Tech posted:Why are you avoiding joins? I wasn't avoiding them, I forgot I could use them.
|
# ? Mar 7, 2010 16:47 |
|
Could someone give me some guidance on a small problem I am having? I have a field in a table which is a timestamp MM/DD/YYYY HH:MM:SS (then AM or PM). I have a view which I need to include this field in, but for my purposes it would be far better to have the field shown simply as MM/DD/YYYY. Seems simple enough but the strings can look like 7/10/2006 or 12/10/2009, so I am unable to simply count from the LEFT reliably as the month field doesnt use leading zeros and the day can be single or double digits. I can't count from the RIGHT because it's a 12 hour clock. I do have one constant, which is the space after MM/DD/YYYY_HH:MM:SS (_ represents the space here). Is there a way I can build it into the view to show what I need?
|
# ? Mar 8, 2010 19:56 |
|
Not sure of your platform but I use DATEPART on MSSQL to pull out specific date sub-elements when I can't be arsed to CONVERT and so on. It seems to work most of the time; however the core problem is likely that your date/time inputs are inconsistent and not reliable, despite whatever magic massaging the SQL server is doing on the column. Here's some documentation: http://www.w3schools.com/SQl/func_datepart.asp
|
# ? Mar 8, 2010 20:02 |
|
Saltin posted:Could someone give me some guidance on a small problem I am having? Is it stored as a DateTime column value or string. Its very easy if its in there as a datetime. Also what SQL are you using ( MySQL i think )
|
# ? Mar 8, 2010 20:09 |
|
This is MSSQL, and the field is type datetime, sorry I should have mentioned that! Thanks for helping out.
|
# ? Mar 8, 2010 20:12 |
|
Saltin posted:This is MSSQL, and the field is type datetime, sorry I should have mentioned that! Thanks for helping out. OKay well that's really quite easy then SELECT CONVERT(char(10),[MyDateField],101) as MyDateField So This is a pretty silly process because they dont have a real date function but it gets the job done. http://msdn.microsoft.com/en-us/library/ms187928.aspx if you want some other formats. Sprawl fucked around with this message at 20:36 on Mar 8, 2010 |
# ? Mar 8, 2010 20:26 |
|
Sprawl posted:OKay well that's really quite easy then EDIT: Spoke to soon. It didnt dawn on me that you did this by converting... the output needs to remain datetime! Saltin fucked around with this message at 21:31 on Mar 8, 2010 |
# ? Mar 8, 2010 20:46 |
|
Saltin posted:Works a treat Sprawl, thanks alot. I see what you did there, always makes sense to me when I see it, can never think of it myself. I'm a SQL noob incase you can't tell : ) Could you do a convert inside a convert? If so, try converting that char convert as a datetime. I'm sure there's an easier way but I don't know MSSQL.
|
# ? Mar 8, 2010 21:46 |
|
Saltin posted:Works a treat Sprawl, thanks alot. I see what you did there, always makes sense to me when I see it, can never think of it myself. I'm a SQL noob incase you can't tell : ) Oh change the Char(10) to datetime.
|
# ? Mar 8, 2010 21:49 |
|
|
# ? May 15, 2024 23:26 |
|
This is an inelegant solution (it's fuckin' retarted) but if all you need to do is strip off the time portion of a datetime field, you can use this:code:
code:
Note that this still always returns the '00:00:00.000' poo poo at the end. You said the output needs to remain as datetime though, so that's what you're stuck with. The datetime data type does not support just mm/dd/yyyy without the hh:mm:ss:xxx. Just the way it goes in the wacky world of SQL Server 2000/2005. Thank god SQL 2008 finally supports date and time as separate data types.
|
# ? Mar 8, 2010 22:23 |