PHP MYSQL - Many data in one column - php

I need to store of 100-200 data in mysql, the data which would be separated by pipes..
any idea how to store it on mysql? should I use a single column or should I make many multiple columns? I don't know exactly how many data users will input.
I made a form, it halted at the part where multiple data needs to be stored.
Anyone know how to store multiple data in single column or is there any alternative way?
please help me..
thank you very much

You should implement your table with an ID for the source of the data. This ID will be used to group all those pieces of similar data so you don't need to know how many you have beforehand.
Your table columns and data could be set up like this:
sourceID data
-------- ----
1 100
1 200
1 300
2 100
3 100
3 200
When you query the database, you can just pull in all of the data with the same sourceID. With the data above, the following query would return two pieces of data.
SELECT data
FROM dataTable
WHERE sourceID = 3
If you have multiple tables, you'll need to associate them with each other using JOIN syntax. Say you have a main table with user data and you want to associate all of this input data with each user.
userID userName otherData
------ -------- ---------
1 Bob xyz
2 Jim abc
3 Sue lmnop
If you want to join data from this table (userTable) with data from the dataTable, use a query like this:
SELECT userID, userName, data, otherData
FROM userTable
LEFT JOIN dataTable
ON userTable.userID = dataTable.sourceID
WHERE userTable.userID = 1
This query will give you all of the data for the user with an ID of 1. This assumes that the sourceID in your data table is using the userID from the user table to keep track of who the extra data belongs to.
Note that this is not the only JOIN syntax in SQL. You can learn about other types of joins here.

Sounds like you need a join table. Have just the data you need in both tables, create a third table with the ID of both tables, then it doesn't matter if you need 100, 200, 300 or more.

If you have a form where this data is coming from, store each input from your form into it's own separate column.
Look for relationships in your data: sounds like you have a "has many" relationship which indicates you may want a linking table where you could do a simple join query...
Storing multiple data in a single column will be a nightmare for queries and updates, unless you're storing XML, event then it would give me nightmares...

Related

Informix generate query from other query (eval results to SELECT) - PHP and Informix

First, just simple question: Is there something as "EVAL()" function in Informix?
And now if anyone is interested there is real world problem where I need EVAL() function:
So, I have two tables in DB:
Fist table (main one) is sales_data(data_id, data_val_id, other, columns, ...)
Second table is for_select(data_id, select_part, from_part, where_part, key_column_name)
Problem is that in first table I don't have all data that I need. Additional data is in other tables depending on "data_id" and "data_val_id" columns in first table.
So, second table is for creating SQL SELECT to get those additional data from other tables.
Now I need to create SQL query like this (but I think that there is no EVAL function in Informix):
SELECT
data_id,
data_val_id,
(SELECT
EVAL(SELECT select_part FROM for_select WHERE data_id=sales_data.data_id)
FROM
EVAL(SELECT from_part FROM for_select WHERE data_id=sales_data.data_id)
WHERE
EVAL(SELECT where_part FROM for_select WHERE data_id=sales_data.data_id)
AND
EVAL(SELECT key_column_name FROM for_select WHERE data_id=sales_data.data_id) = sales_data.data_val_id
) AS additional_data
FROM
sales_data
I don't know who came up with this idea but it is not mine and I can't chage it...
I did tried to do this in PHP with dynamic SQL queries but it is veeery slow. For every row in results from main table PHP must connect to server and send new query. It is very slow and in main table I have around 50000 rows and I have 4 more this "additional_data" columns (grand total 5 "data_id" and "data_val_id" columns in main table).
Can someone help me? Is this posible in one SELECT? Is there something as "EVAL()" function in Informix?
-------- more explanations ---------
Every row in for_select table is very diferent from each other. Data in sales_data table is from our selling (our job) and final report (joined main and additional data) must have all columns. This is something that we can't change, report must look how it looks now and must have all data that have now. I just need to find a way to export it quicker than now.
I will try to write example: In DB we have tables for warehouses, for goods, for clients, for workers, for cars, ... and every row in for_select table is for one of this tables. Table for_select have 54 rows, so this means that we have 54 tables that we need additional data from to put in final report.
In sales_data we only have 5 columns that can be used for 5 diferent things depending on data_id column but I have 54 posible "things" that can be there, so comination of data_id (what table is used for "additional data") and data_val_id (what row from that table) determine what will be in final report. In real life data_id is for diferent type of goods that we sell and therefore for every diferent goods we need to show diferent "additional data" in addition to basic data: number of selled items, price,...
So, why we need for_select table, you ask? Because every table for additional data have diferent names for columns and in diferent place. Table cars have car_id, car_model columns but table warehouses have wh_id,wh_location and wh_name columns, from cars table I ony need car_model column but from warehouses I need wh_location||wh_name together (concated).
Tables names and data are in Serbian and names are not informative at all, so I did my best to translate it to English so you all can understand. Raw data will just confuse you all...

MYSQL output multiple rows with just a single row in mysql database

I have this data that should output to corresponding number of social media that he interacted with.
There's 4 interaction which is fblike_point, fbshare_point, tweet_point, and follow_point
So let's say, I've interacted with fblike_point and tweet_point judging from the data below.
So what I want to do is, it should output 2 times since I've interacted with fblike_point and tweet_point.
Output:
2013-05-14 | fblike_point
2013-05-14 | tweet_point
If I interacted 4 times, it should output 4 times with the corresponding social media interaction that he made.
Well I can manage to do this stuff but, it was like redundancy, for example I'm using a mysql query in PHP for selecting data:
SELECT date_participated, fblike_point FROM table WHERE fblike_point = 1
SELECT date_participated, fbshare_point FROM table WHERE fbshare_point = 1
SELECT date_participated, tweet_point FROM table WHERE tweet_point = 1
SELECT date_participated, follow_point FROM table WHERE follow_point = 1
So is there any other way to have a short method or something?
If I interacted 4 times, it should output 4 times
With your data schema, you'd either need the four distinct queries you quoted, or a UNION over these.
it was like redundancy
This is redundant because the way your schema is organized. If you want to be able to treat these different interactions alike (which makes a lot of sense), then you'd want an extra table for these, with one column identifying the row of your original table that this refers to, and a second column (probably of an ENUM type) identifying the social media. Both together would form the primary key of that table.
You can then create a VIEW from the actual tables which looks just like your table does now. That way you can maintain compatibility to existing queries and still provide more flexible queries for those cases where you need them.

MySQL In query using results from another query?

I need some help creating a query for my mySQL database. I have recently started using JOIN and IN to select rows from my database, so I apologize for sounding like a noob.
I am not looking for an answer (although one would be nice!) but advice on where I should start with my query would be greatly appreciated. Please do not just post a link to the PHP website or to a tutorial.
In my database, users are "linked", the order is NOT important.
Table Name: UserLinks
link_ID User_1 User_2
1 10982* 34982
2 82738 16643
3 10982* 99822
4 78256 10982*
The user that I am focusing on here is user 10982
1) The first part of this query that I would like to create find the ID of the users that this user (10982) is linked to. These users are user 34982, 99822, and 78256. I imagine it is something as simple as SELECT * FROM UserLinks WHERE User_1 OR User_2 = 10982, my issue here is, how do I obtain the values - given that I cannot simply choose a row to return (I considered using an if statement... if the value of User_1 is 10982, then choose User_2. This however, seems redundant.
With this list of Users ID's, I would like to run the second part of the query, to find which Event_ID's correspond to this list of users (the ID's of the users are now UserEv_ID):
Table Name: EventUserTags
ETag_ID UserEv_ID Event_ID
1 34982* 289
2 82738 231
3 99822* 990
4 78256* 486
2) The second part of this query will use the list generated from the first query (of user ID's) to generate a list of Event_ID's. I know that you can use the IN statement and just dump this list in to the query as an array, but that means making the results of the first part of the query into an array. This again, seems redundant. I would like to know how to properly select these Event_ID's using the User ID's... I think that the JOIN Query will work, but I need some advice on how to use this.
The values of Event_ID's obtained here are 289, 990, and 486. In last part of this query I need to use this list of Event_ID's generated from the last query to match up with the data of another table, my Events table.
Table Name: Events
Event_ID Event_Order
182 8728342
289 3478792*
990 1876623*
486 9617789**
3) Lastly, I need to use the Event_ID's obtained from the last query to obtain their corresponding Even_Order. Again, I know this can be done with the IN statement (using an array) but this will not be efficient at all.
The purpose of this query is to start with a single User's ID, and find the Event_Order of every user this user is linked to.
Any help is really appreciated
This can all be done fairly simply in one query which I will write and then explain:
SELECT
Event_Order, -- I think this is the one you need, but selecting other fields anyway
Event_ID,
User_1,
User_2
FROM
Events
NATURAL JOIN EventUserTags
JOIN UserLinks ON (
UserEv_ID = User_1 AND User_2 = 10982
OR UserEv_ID = User_2 AND User_1 = 10982
)
NATURAL JOIN is a short cut for JOIN EventUserTags ON (Events.Event_ID = EventUserTags.Event_ID). It works because the keys you are joining on have the same name.
Then you join on the respective user IDs only if the link has the user you're looking for.

Fetching records from different tables in the database

My application has a facebook-like stream that displays updates of various types. So it will show regular posts (from the "posts" table), and events (from the "events" tables) table and so on.
The problem is I have no idea how to fetch these records from different tables since they have different columns. Shall I query the database multiple times and then organize the data in PHP? if so, how? I'm not sure how I should approach this.
Your help is much appreciated :)
Unless the events and post are related to each other, then you'd probably query them separately, even if they show up on the same page.
You're not going to want to use JOIN just for the sake of it. Only if there is a foreign key relationship. If you don't know what that is, then you don't have one.
If the data tables are related to each other you can generally get the data back in a single query using some combination of JOINs and UNIONs. For a better answer, however, you'll have to post the structure of your data tables and a sample of what (combined) records you need for the website.
If you don't know the columns, you can get the table meta-data and find out what the columns represent and their corresponding data types.
If you know which columns, you can select from the multiple tables or even use nested selects or joins to get the data out.
Ideally you'd simply use a JOIN to obtain data from multiple tables in one query. However, without knowing more about your table schemas it's hard to provide any useful specifics. (It most likely won't be possible unless you've factored this in from the beginning that said.)
As such, you might also want to create a generic "meta" table that provides information for each of the posts/events in a common format, and provides a means to link to the relevant table. (i.e.: It would contain the "parent" type and ID.) You could then use this meta table as the source for the "updates" stream and drill down to the approriate content as required.
Join the tables on user_id i.e.
Select * from posts p
left join status_updates su on p.user_id = su.user_id
limit 25;
or if your tables differ too much then play with a temporary table first
create table tmp_updates
(
select user_id, p.id as update_id, 'post' as update_type, p.text from posts;
);
insert into table tmp_updates
(
select user_id, su.id as update_id, 'status' as update_type, su.text from status_updates;
);
Select * from tmp_updates
where user_id = '...'
limit 25;

Questions about Php and Mysql Hash Table

I am a new php and mysql programmer. I am handling quite large amount of data, and in future it will grow slowly, thus I am using hash table. I have couple of questions:
Does mysql have hash table built in function? If yes, how to use that?
After couple of days doing research about hash table. I briefly know what hash table is but I just could not understand how to start creating one. I saw a lot of hash table codes over the internet. Most of them, in the first step in to create a hashtable class. Does it mean, they store the hash table value in the temporary table instead of insert into mysql database?
For questions 3,4 & 5, example scenario:
User can collect items in the website. I would like to use hash table to insert and retrieve the items that the user collected.
[Important] What are the possible mysql database structure looks like?
e.g, create items and users table
in items table have: item_id, item_name, and item_hash_value
in users table have: user_id, username, item_name, item_hash_value
I am not sure if the users table is correct?
[Important] What are the steps of creating hash table in php and mysql?
(If there is any sample code would be great :))
[Important] How to insert and retrieve data from hash table? I am talking about php and mysql, so I hope the answers can be like: "you can use mysql query i.e SELECT * from blabla..."
(sorry about the italics, underscores can trigger them but I can't find a good way to disable that in the middle of a paragraph. Ignore the italics, I didn't mean to put them there)
You don't need to worry about using a hashtable with MySQL. If you intend to have a large number of items in memory while you operate on them a hashtable is a good data structure to use since it can find things much faster than a simple list.
But at the database level, you don't need to worry about the hashtable. Figuring out how to best hold and access records is MySQL's job, so as long as you give it the correct information it will be happy.
Database Structure
items table would be: item_id, item_name
Primary key is item_id
users table would be: user_id, username
Primary key is user_id
user_items table would be: user_id, item_id
Primary key is the combination of user_id and item_id
Index on item_id
Each item gets one (and only one) entry in the items table. Each user gets one (and only one) entry in the users table. When a user selects an item, it goes in the user items table. Example:
Users:
1 | Bob
2 | Alice
3 | Robert
Items
1 | Headphones
2 | Computer
3 | Beanie Baby
So if Bob has selected the headphones and Robert has selected the computer and beanie baby, the user_items table would look like this:
User_items (user_id, item_id)
1 | 1 (This shows Bob (user 1) selected headphones (item 1))
3 | 2 (This shows Robert (user 3) selected a computer (item 2))
3 | 3 (This shows Robert (user 3) selected a beanie baby (item 3))
Since the user_id and item_id on the users and items tables are primary keys, MySQL will let you access them very fast, just like a hashmap. On the user_items table having both the user_id and item_id in the primary key means you won't have duplicates and you should be able to get fast access (an index on item_id wouldn't hurt).
Example Queries
With this setup, it's really easy to find out what you want to know. Here are some examples:
Who has selected item 2?
SELECT users.user_id, users.user_name FROM users, user_items
WHERE users.user_id = user_items.user_id AND user_items.item_id = 2
How many things has Robert selected?
SELECT COUNT(user_items.item_id) FROM user_items, users
WHERE users.user_id = user_items.user_id AND users.user_name = 'Robert'
I want a list of each user and what they've selected, ordered by the user name
SELECT user.user_name, item.item_name FROM users, items, user_items
WHERE users.user_id = user_items.user_id AND items.item_id = user_items.item_id
ORDER BY user_name, item_name
There are many guides to SQL on the internet, such as the W3C's tutorial.
1) Hashtables do exist in MySQL but are used to keep internal track of keys on tables.
2) Hashtables work by hashing a data cell to create a number of different keys that separate the data by these keys making it easier to search through. The hashtable is used to find what the key is that should be used to bring up the correct list to search through.
Example, you have 100 items, searching 100 items in a row takes 10 seconds. If you know that they can be separated by type of item and break it up into 25 items of t-shirts, 25 items of clocks, items rows of watches, and items rows of shoes. Then when you need to find a t-shirt, you can only have to search through the 25 items of t-shirts which then takes 2.5 seconds.
3) Not sure what your question means, a MySQL database is a binary file that contains all the rows in the database.
4) As in #2 you would need to decide what you want your key to be.
5) #2 you need to know what your key is.
If you think a hash table is the right way to store your data, you may want to use a key-value database like CouchDB instead of MySQL. They show you how to get started with PHP.
I am a new php and mysql programmer. I am handling quite large amount of data, and in future it will grow slowly, thus I am using hash table.
lookin at your original purpose, use "memcache" instead, it is the most scalable solution while offers the minimal changes in your code, you can scale up the memcache servers as your data go larger and larger.

Categories