I not that knowledgeable with PHP. I've spent a good portion of my day researching and I resort to you for help. I will try my best to articulate my setup and issue. I am creating a table that will display certain data from my database and I am a using 'tablesorter' jquery plugin.
+-----+------------+---------+
| id | m_key | m_value |
+-----+------------+---------+
| 262 | last_name | Bore |
| 262 | first_name | Dan |
| 261 | last_name | Bore |
| 261 | first_name | Dan |
| 255 | last_name | Nez |
| 255 | first_name | Bob |
| 250 | stock | 50 |
| 250 | price | 102 |
+-----+------------+---------+
^ Database Table
mysql_query("SELECT id,m_value FROM table WHERE m_key='last_name'")
The database table I am working with isn't as clean as I hoped for. The query line here does the filtering. It grabs any ID and m_value which has last_name as one of the values from m_key. Here is proof I have it working http://cl.ly/image/47461i2e412j.
while($row = mysql_fetch_array($rs)) {
echo "<tr><td>$row[id]</td><td>$row[m_value]</td><td>$row[m_value]</td></tr>\n";
}
This is the part I am stumped on. Most of my important data is under the m_value column. Easy enough the first $row[id] from the array successfully displays the ID and the next $row[m_value] seems to attribute itself to last_name. I would like the proceeding $row[m_value] to attribute the first_name. How can I go about specifying this through code?
Related
I've got a small CRM and I'm trying to figure out the best way to designing the DB tables.
I've currently got a single table for users that got around 30 columns which I alter from time to time. Since I am storing two different information on that table (user + company information) I was thinking of splitting that table into 3 (user + company + connection between these 2) but I am also interested in keeping a copy of any changes that are being made in these rows.
So going from:
user_id | firstname | last_name | company_name | company_city | company_subject | rank | status
1 | John | Borrows | Boink INC | NY | Web dev | 1 | 1
2 | Mike | Smith | Smithin INC | OC | Laywer | 1 | 2
3 | Mary | Anton | Caffin | SJ | Moving | 2 | 1
to something like this
user_id | firstname | last_name | rank | status
1 | John | Borrows | 1 | 1
2 | Mike | Smith | 1 | 2
3 | Mary | Anton | 2 | 1
comp_id | company_name | company_city | company_subject
1 | Boink INC | NY | Web dev
2 | Smithin INC | OC | Laywer
3 | Caffin | SJ | Moving
con_id | user_id | comp_id
1 | 1 | 1
2 | 2 | 2
3 | 3 | 3
But I'm not sure how to track the changes when for example a user changes the company name or some other info on user's table etc.
Just follow the normalization rules for structuring your database tables. You will find anything you need for that by just searching for database normalization.
Regarding your "update-history" you could add a Timestamp to your datasets and/or a separate boolean field "outdated" to be able to filter out the latest information.
Would be the simplest solution that comes into my mind.
Is it possible to auto insert a record into the primary table if the record does not exist when adding a foreign key?
For example, assume these tables:
- user(id, name, age)
- topic(id, name)
- post(userId, topicId, text, createdAt, updatedAt)
Now i am pulling posts from some source and saving the records in the post table. But sometimes the data that is being returned contains a userId or a topicId that is not yet in my database. So everytime i would have to check if the user and topic records exist then save if not. Only then my post record would be valid and saved.
I want to be able to save the post even if its related user or topic does not exist, and add an empty row with the in these tables having the ids that have been stored in the post table.
Example:
Current User Table
+----+------+-----+
| id | name | age |
+----+------+-----+
| 15 | Paul | 26 |
+----+------+-----+
| 56 | John | 31 |
+----+------+-----+
current Topic Table
+----+----------+
| id | name |
+----+----------+
| 5 | Business |
+----+----------+
| 12 | General |
+----+----------+
current Post Table:
+--------+---------+----------------+-------------+-------------+
| userId | topicId | text | createdAt | updatedAt |
+--------+---------+----------------+-------------+-------------+
| 15 | 12 | blah blah blah | *timestamp* | *timestamp* |
+--------+---------+----------------+-------------+-------------+
| 56 | 5 | lorem ipsum... | *timestamp* | *timestamp* |
+--------+---------+----------------+-------------+-------------+
So then i fetch post from some sources an get a new 1 This is a new topic posted by a user with id 72 in a topic with id 2. The source only returns the id, and to obtain the rest of the details of the user, i should make another request to their api.
Post Table after:
+--------+---------+---------------------+-------------+-------------+
| userId | topicId | text | createdAt | updatedAt |
+--------+---------+---------------------+-------------+-------------+
| 15 | 12 | blah blah blah | *timestamp* | *timestamp* |
+--------+---------+---------------------+-------------+-------------+
| 56 | 5 | lorem ipsum... | *timestamp* | *timestamp* |
+--------+---------+---------------------+-------------+-------------+
| 72 | 2 | This is a new topic | *timestamp* | *timestamp* |
+--------+---------+---------------------+-------------+-------------+
User Table After:
+----+------+-----+
| id | name | age |
+----+------+-----+
| 15 | Paul | 26 |
+----+------+-----+
| 56 | John | 31 |
+----+------+-----+
| 72 | | |
+----+------+-----+
Topic Table after
+----+------------+
| id | name |
+----+------------+
| 2 | |
+----+------------+
| 5 | Business |
+----+------------+
| 12 | General |
+----+------------+
So now that i have this, i can make my request to their api and look for data for user with id 72 and data for topic with id 2.
People can have strong opinions on this and we can respectfully disagree.
In reference to a comment saying people do this (knowingly loading blank and null junk in tables) all the time as seen in the post Here.
I said:
That reference is a consortium of people out of their minds. Writing a
post saying what people want to hear, putting a lollipop in their
mouths, does not make for a decent answer. In fact, it can be pretty
irresponsible. This OP is doing things in the wrong order. Put stuff
in some staging tables, call the other APIs, get stuff in clean, that
makes me sleep well at night. Referential Integrity has a meaning. We
don't twist it and confuzzle everyone just to please them.
Part of our responsiblity is doing the right thing, in the right order, to keep our data clean and supporting Referential Integrity. And to steer our peers toward the same versus anything contrary. Sort of the Prime Directive.
i want to ask how to get the value of the column and update into another column with same id..
this data came from email piping and i want to separate the value of the body and update into the expense , net and gross field..
i don`t know how to do it.. i hope you can help me guys.. thank you
here is my table
+------+---------+--------------------+---------+----------+-------+----------+
| id | subject | body | expense | net | gross | email |
+------+---------+--------------------+---------+----------+-------+----------+
| 1 | Sales | Expense = 2000 | 0 | 0 | 0 | ex#ex.com|
| | | netIncome = 5000 | | | | |
| | | Gross = 10000 | | | | |
+------+---------+--------------------+---------+----------+-------+----------+
try something like this:
UPDATE table_name SET expense = LEFT(body, LOCATE("netIncome",body))
I am a newbie to redis.
Here is my doubt.
Let's say I have 100 records from a MySQL table.
id | name | surname | age | username | password | amount | currency
1 | nick | goean | 29 | nick_go | nickpass | 120
2 | joe | keve | 30 | keve_joe | kevepass | 110
I need to store this in redis and should use to query by age and currency,
actually I should get all the possible combinations to query from redis.
Can anyone help me with an example data structure for the same?
I have 14 tables (one for every year) with product code, firm name and invoice numbers. Main structure of table is identical (product code, ID), but there can be some variables in names of firms.
Table2011
| ID | productcode | firm1 | firm2 | firm3 | etc |
| 1 | G-00001 | 2;5;40| 32;67 | | 150 |
| 2 | G-00005 | | 50 | | |
|etc | | | | | |
Table2010
| ID | productcode | firm1 | firm2 | firm3 |etc |
| 1 | G-00001 | 1;10 | | 55 | |
| 2 | G-00003 | | 2 | | |
| 3 | G-00005 | | 50 | 40 | |
| etc| | | | | |
Table2009
...
Column Firm1 do not usually equals to same firm as firm 1 in other table
I am using table editor to work with tables (adding columns to table, editing values…).
I would like to know if it is possible to achieve result like below. It is above my PHP skills.
Product G-00001 page
…
<UL>
<LI>Year 2011: 150etc; 67firm2; 40firm1; 32firm2; 5firm1; 2firm1</LI>
<LI>Year 2010: 55firm3; 10firm1; 1firm1</LI>
<LI>Year 2009: ...</LI>
...
</UL>
…
Lemme begin with book recommendation : SQL Antipatterns. You will need it, doesn't matter if you caused this mess or ar just assigned to fix it.
If i was in your place, first thing would do would be to fix the database structure. This is madness. You do not need a new table for each year and new column for each company. Database is not a form of Excel spreadsheet.
Invoices Years Companies
----------------- ------------- ---------------
| product_code PK | | year_id PK | | company_id PK |
| company_id FK | | number | | title |
| amount | ------------- ---------------
| year_id FK |
-----------------
Where PK - primary key and FK - foreign key.
This structure would make the gathering of information much much much MUCH easier.
If you just want to display the data and not worry about the restructuring just yet you can use a JOIN to display the information from all the tables.
Although I would agree with teresko you really need to redesign that database. It is not maintainable the way it is.