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`
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a table named Pets that has a column of owner_name. The user wants to put the owner's name and it will suggest the possible owner names kinda like if I put Jo on the search box, it suggests John,Jonathan,Joshua,Jorge. But the problem is, there are times when an owner has multiple columns under their name with different pets. Kinda like John owns a cat,dog,bird. My program does the search suggestion but the problem is that it repeats the names, so if I typed Joh it lists three Johns on the auto suggest. Out of curiosity I tried the LIMIT 1 but as expected, what it did was limited the entire results to 1 so if I put Jo, it just displays John.
Use the DISTINCT keyword. Try something like:
SELECT DISTINCT columnname
FROM query
WHERE ....
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I want to display all data that are related to each other.
For example, I have a sales_item table
itemid | srfno | qty | serial | description
1 |1234 |1 |354 |laptop
2 |1234 |2 |456 |iphone
I want to display the 2 qty, serial and description since they both belong to the same srfno. I can only output the first one. Pls help me.
my select statement to count all the same srfnos
Select COUNT(sales_item.srfno) as srfno from sales_item;
getting data from db:
$exqty=$row['exqty'];
$exserial=$row['exserial'];
$exdesc=$row['exdesc'];
I output it using:
<input name=qty value="<?php echo $exqty ?>"/>
if you issue the following select statement:
Select COUNT(sales_item.srfno) as srfno from sales_item;
you will get a row array in php like this:
Array (
0 => Array (
"srfno" => 2
)
)
Since the result does not contain any member with the name exqty the statement $row['exqty']; will end in an error undefined index "exqty".
Either you adjust your Select statement ("select * from sales_item") or your php code - depends on what you're trying to achieve.
Besides:
Select COUNT(sales_item.srfno) as srfno from sales_item;
will run into problems as your expression COUNT(sales_item.srfno) is named the same as the column itself. Better:
Select COUNT(sales_item.srfno) as srfno_count from sales_item;
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 have been trying, but I cannot figure this myself...
I have a MySQL table that contains info like this (plus an ID column):
Artist Song Uploader
art1 song1 name1
art2 song2 name2
art1 song1 name3
I would like to use php to present this table with this output:
Artist Song Times uploaded
art1 song1 2
art2 song2 1
The main idea is to find the duplicates (artist + song) inside my table.
I have tried using array_count_values() with the variable that contains all the data from the MySQL table. But this outputs this error:
Warning: array_count_values(): Can only count STRING and INTEGER values! in index.php on line 40
I'd really appreciate some help.
Thanks all!
Don't do this in PHP, do it in your database query
SELECT artist,
song,
count(id) as uploadCount
FROM mytable
GROUP BY artist,
song
Let the database do what databases are good at doing
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...