Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Such a good day, everyone is well, the question is the following.
I think a couple of tables in mysql, I sometimes get data from both research I come across the typical "link two tables", "make two queries to two tables", but this has me confused.
That is, if we have two tables, as follows:
Tabla1
| id | nick |
|-------------
| 1 | admin|
tabla2
| id_post | content | autor |
|---------------------------------
| 100 | asdasd | 1 |
Why relate from mysql, but when you query you can do:
select tabla1.nick, tabla2.* from tabla1, tabla2, where id="1" and tabla2.autor = tabla1.id
What is the difference between the two?, Or what is the benefit to having one or the other?
If I understand your question correctly, you're asking about constraints, why actually make column X on table A refer to column Y on table B, when you can just join the two tables in a SELECT query?
This is to enforce referential integrity, to reduce redundancy, etc. Doing so makes the data itself reliable so that when you use joins in your SELECT statement, they work as they should.
If you had an ASSIGNMENTS and a SUPERVISORS table, for instance, and each assignment is always assigned to a supervisor on a supervisors table, a foreign key constraint between the supervisor field on ASSIGNMENTS and SUPERVISORS will ensure that happens. It also gives you flexibility as to what should occur if the supervisor value changes on one table (should it be restricted? should the change be carried through to the other table? etc.)
Without the relationship being defined, an assignment might be assigned to a supervisor who does not even exist. And then the results of your SELECT statements won't be all that reliable...
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I am creating a system where users (who are identified by a user id number) will be allowed to vote on posts (think Reddit, StackOverflow, etc).
Users can vote a post up or not vote at all on it.
The number of votes on a given post can easily be stored within the table containing the posts.
Keeping track of who has voted, however, is a different task entirely that I'm not sure how to approach.
I was thinking I could have a table that would have two columns: user id and post id.
When they vote on a post, I add their user id and post id to that table. If they unvote, I remove that entry from the table.
EG:
User ID | Post ID
1 | 3949
1 | 4093
2 | 3949
etc...
Is this a reasonable solution?
Yes this is reasonably simple and easy solution to the problem. You can do the same for your comments(if you like to). In your MAIN_POST table assign a post_id and use this same post_id in other tables (comments(post_id, user_id, post_comment, comment_time) and votes(post_id, user_id, vote_status(you can use 1 for vote up and 0 for vote down))). It will complicate your sql queries, to retrieve data, a little but you can do it. And on android side there are alot of tricks to handle and furnish this data in application and you can make this vote(like) and comments idea just like facebook (YOU for your comments and likes and NAMES for others).
I wouldn't remove rows from the table. I understand why you would want to do that, but why lose the information? Instead, keep a +1/-1 value for each entry and then sum up the values for a post:
select sum(vote)
from uservotes
where postid = 1234;
And, I agree with Rick that you should also include the creation date/time.
Using an 'in between' or 'joining' table is a perfectly acceptable solution in this case. If relevant you could even add a timestamp to the relation and show to the user when a user has upvoted something.
Also it is important to take care of proper Indexes and Keys to have your table structure also perform properly once the dataset grows.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am currently trying to get values from tables I have in a webpage into a database using PHP. However, the order of the values inside each box is important, so I want the first box to be ranked 1 and the second box to be ranked 2 and so on... There is no limit to the number of items in each box. There may be as many as 100 or 0. Each item in each box is dragged from a bank of items into the table, which represents a topic. The tables are the output of the interface.
So example table:
Rank1
Rank2
Rank3
I've currently tried dumping the entire page once the user fills it in into a text file and parsing it from there but i'm looking for a more functional and practical way of doing it.
If I understand your goal correctly, you have two sections on your webpage. A word bank and a <table>. A user can drag and drop items from the word bank to a cell in your <table>. Next you want the order of the contents of the cell to be preserved when the data is saved to a database-table.
Given the description you have provided, it seems you may be new to using databases. It is not a problem but you will need to do some study on how databases work and how to use them.
Again, if I understand correctly, your <table> looks like the following:
| topic-1 | topic-2 | topic-3 |
-------------------------------
| ---A--- | ---J--- | ---V--- |
| ---B--- | ---J--- | ---X--- |
| ---C--- | NULL | ---Y--- |
| NULL- | NULL | ---Z--- |
To move this into a database you need to create a database, and some table, for example MyDataTable. Next that table may have three columns: id, contents, category.
Here is some info on Mysql Insert.
INSERT INTO MyDataTable(`id`, `contents`, `category`)
VALUES (1, A, Topic-1)
You will need to format that call to work correctly via php, but that is the general idea. In your php code you will need to iterate over the elements in the order you want, and you can use the id to refer to your order... Alternatively you could add another column to your database-table and have that refer to your order, which would allow you to use some other value, perhaps auto-increment, as the primary key.
I would recommend becoming comfortable with MySQL if you aren't already, before tackling your project's specific needs. Namely, INSERT, DELETE, UPDATE, SELECT and WHERE will almost certainly be necessary MySQL functions.
One resource I found very helpful in grasping MySQL is this book, SQL Antipatterns: Avoiding the Pitfalls of Database Programming .
Using the phpMyAdmin page can be very helpful in debugging SQL queries, once the query syntax is correct, you can translate it to php and incorporate your dynamic variables. Also if you store your sql-syntax in php as $sql, using var_dump($sql) can also be a great help when debugging sql syntax.
In programming, iterate over, typically refers to creating a for-loop to iterate, or step through 1-by-1, each of the items of an array or similar container.
$MyArray = array();
$MyArray = GetTableContents(); //You have to write this function.
for ($idx = 0; $idx < count($MyArray); $idx++)
{
// do my stuff, like, Insert into my DB-table.
}
In order for the code above to work, you need to fill MyArray with the contents from your <table>. In the example code above, the $idx, could be used as your id.
In addition to MySQL, it seems it would also be worth your time to learn more about php programming. Read simple tutorials, how-to's, and books. There are numerous resources on the internet about php. One such example is here.
I hope this is helpful.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm partially familiar with the COUNT function, but I'm a little stumped as to how I can apply it to this specific situation. Suppose I have a table with one of the columns containing the following values:
apples
apples
apples
oranges
oranges
bananas
These values can be added by users, so I never know what values may end up in this column, but there will always be duplicates. What I first want to do is to display them in a HTML table, so that each row will represent one particular value, and each adjacent cell shows the number of duplicate values. Using the example data above, the expected output in a HTML table generated by PHP would be:
|Fruits | Amount |
|-----------|----------|
|apples | 3 |
|oranges | 2 |
|bananas | 1 |
At this stage, I know that I have to use the COUNT function in SQL, and through PHP, I need to run some sort of foreach loop, but beyond that, I'm not sure how to proceed.
EDIT: Changed the data. Hopefully it makes a little more sense
SELECT [column_name], COUNT([column_name])
FROM [table_name]
GROUP BY [column_name];
Add GROUP BY valueName; to the end of your query.
Select `column name`, count(*) from `table1`
group by `column name`
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Hi I am new to web technology (Well not advanced). I am trying to build an online store (computer hardware) with mysql and PHP, I am wondering how to add a search functionality (not google's).
I am planning to make a search bar where visitors can enter key word or key words for search.
The search for these key words should span many tables with totally different content.
I know about SQL syntax, I have a good understanding of REGEXPs, I am good with indexes and views...
The only thing I want is guidance, a general idea.
you should first design your database. then make website design and program it in PHP.
as far as search functionality concerns you should make something like that,
eg.
database and tables and their columns etc..
for example, if you have one table named hardwares
+--id---+---Name----+---Cost----+-Warrenty--+
+-------+-----------+-----------+-----------+
| 1 |hardware1 | 2000 | 2 |
| 2 |hardware2 | 5000 | 1 |
| 3 |hardware3 | 5000 | 3 |
+-------+-----------+-----------+-----------+
then in coding part of the website there will be query fired something like that,
select * from hardwares where Name LIKE '%$search_input%`
here, the search input is taken from the user and this query will result the particular hardwares' information and then from the results you can get ID of that hardware which is already stored in that table.
from that ID, you can make a page that will accessed by a particular query for example,
http://www.yourwebsite.com/hardwares.php?id=2
this page will load that particular hardwares' page and it will have all the information regarding to that hardware.
Search from catalog items - this is search for database.
MySQL code
SELECT nameItem FROM catalogItem WHERE `nameItem` LIKE '%search phrase%' OR `descriptionItem` LIKE '%search phrase%'
This is the simplest example.
A architecture of search I would do with caching results in a separate table.
PS See how to implement search in popular CMS
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
As part of a bigger project, I'm building a reporting application which analyses business data. The aim is to have default datasets and also allow for users to upload their own datasets.
I'm building this in PHP however I'm not sure what the best database is to use. MySQL is great and usually my first pick, but this is application does not really require a relational database for the datasets. Datasets could be thousands of columns and millions of rows. Getting this to work MySql is quite a challenge.
I've seen HDF5 which seems like a good database though not very well supported from a web application point of view.
Do you have any other suggestions for a database that can store such large datasets?
P.S - I should clarify the reason for the title.... I need to be able to allow users to upload datasets. The only way with MySQL would be to allow for a table to be created each time the users uploads a dataset. Doesn't really sound very good.
You should look into NoSQL databases. That sounds like what you are looking for. With NoSQL you don't have to create databases but store what ever and still be able to search the data fast.
If I understand you correctly, you need to create a sub table ( or something similar to this ) and store the custom fields in there:
Table name: custom_fields
+---------+----------------+----------+
| user_id | field name | value |
+---------+----------------+----------+
| 10 | favorite_sport | hockey |
| 10 | favorite_food | hot-dogs |
| 20 | sisters_name | Ashley |
+---------+----------------+----------+
And after this, just join the data together and show what you need.