• Jetzt anmelden. Es dauert nur 2 Minuten und ist kostenlos!

Arrays in MySQL

Jeremygolf

Mitglied
I'm working on a website to create golf statistics. And I need to store rounds, and every round has 9 or 18 holes. So for example I want to save every score of each hole, how would I do that. One round should be one database entry. In MySQL I can't create arrays. I want to use MySQL how can I do that? Or are there better backends?

Example:

On every hole I make a random score between 2 - 6 Shots. Every hole I need to check if I hit the green (yes/no).
So entry should be something like this:
greenhits: yes, yes, yes, no, yes, no, yes, yes, no ...
score: 5, 4, 5, 3, 2, 4, 4, ....

And all this should go into one single database entry. How can I do that with mysql?

Thank You
 
Hello,

You should think about why you are using tabels in SQL -> you use it to save large data and this the same reason why you use arrays in programming. Tables store relative data within a database in tabular format which allows to access and analyse the data very easy. The main difference is normally that mysql does't have a temporary memory and are saved in local file systems.

So tables and arrays are quit the same. We can try to store every array element in its own record in the DBMS.

But if you really want to store all the information in 1 database entry, you will have do use varchar and build a string first before you storing the data in the database. For analysing you will have to create an array out of the recorded string at runtime :-)
 
Hello,

You should think about why you are using tabels in SQL -> you use it to save large data and this the same reason why you use arrays in programming. Tables store relative data within a database in tabular format which allows to access and analyse the data very easy. The main difference is normally that mysql does't have a temporary memory and are saved in local file systems.

So tables and arrays are quit the same. We can try to store every array element in its own record in the DBMS.

But if you really want to store all the information in 1 database entry, you will have do use varchar and build a string first before you storing the data in the database. For analysing you will have to create an array out of the recorded string at runtime :)
Thanks for your answer. But In golf statistics there is not just green hits an scores, there are minimum 10 other criteria, so there would be too much tables. I think storing everything in archer or text is a good idea. How do I covert this?

Regards
 
You will probably have a table listing every golfer. There you could include a row wich includes a foreign key to the statistic table. There i would save every criteria seperarly in a string with a seperation comma.

To convert the array in a string an vise-versa it depends on the language you are using for your backend. In PHP for example you would need implode and explode to achieve this.
 
String mit Komma? (Ich hab mir mal erlaubt auf Deutsch zurückzukehren, weil hier beide Gesprächsteilnehmer normalerweise auf Deutsch gepostet haben...)

Einfacher wäre es doch alle statistiken in einer Zeile festzuhalten. Green (y/n), etc...

Die Berechnung dann in php.

Und den Golfer kann mal als foreign key anhängen, wie Nitamud bereits erwähnt hat.
 
String mit Komma? (Ich hab mir mal erlaubt auf Deutsch zurückzukehren, weil hier beide Gesprächsteilnehmer normalerweise auf Deutsch gepostet haben...)

Einfacher wäre es doch alle statistiken in einer Zeile festzuhalten. Green (y/n), etc...

Die Berechnung dann in php.

Und den Golfer kann mal als foreign key anhängen, wie Nitamud bereits erwähnt hat.

Eine Runde hat aber 18 Löcher. habe es bis jetzt mal so gelöst. Hast du eventuell eine besser Idee?

upload_2015-1-22_9-31-44.png
 
Eine weitere Tabelle für jedes Loch mit einem foreign key auf die Runde. Nur so kannst du eine Normalform erreichen.

Eine andere (sehr unschöne) möglichkeit wäre es, score1 bis score18 hinzuzufügen, ich würde das jedoch nichtmal in betracht ziehen, da es wie geschrieben, sehr unschön ist.
 
Eine weitere Tabelle für jedes Loch mit einem foreign key auf die Runde. Nur so kannst du eine Normalform erreichen.

Eine andere (sehr unschöne) möglichkeit wäre es, score1 bis score18 hinzuzufügen, ich würde das jedoch nichtmal in betracht ziehen, da es wie geschrieben, sehr unschön ist.
Das wird doch sehr mühsam mit einlesen und auslesen der Daten?!?!? Bis jetzt benutze ich implode und explode und das funktioniert bis jetzt ganz okay.
 
Ich will dir nicht vorschreiben wie du es tun sollst, ich sage nur, wie es korrekt ist und wie es "normalerweise" gemacht wird und werden sollte.

Ob du es dann so umsetzt ist dir überlassen
 
Zurück
Oben