MySQL query to get one of each Item [closed] - php

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 7 years ago.
Improve this question
I am working with php and MySQL, I am making a messaging app, the app has communicates with a remote database, in my database is my messaging table, the table has 9 tables of which includes the "Subject table", I need help, I want to make a query that retrieves only one subject of each type, lets say I have 10 messages with the subject "Man", 12 of the "Dog", I want it to get only one man and one dog below is a graphic representation of my Messaging Table.
| message_id | subject | username |
|:-----------|------------:|:------------:|
| 1 | cyber | Chrome |
| 2 | Hyper | Ciare |
| 4 | Cyber | Gorger |

You can use SQL's DISTINCT:
SELECT DISTINCT subject FROM table
This will fetch each subject only one time, even if it appers more than once.

Related

Delete and Insert vs Update rows on table [closed]

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 5 years ago.
Improve this question
I have a web form in when the user can select many available items (idFamilia) for a single Provider (idProveedor) that looks like this
The values are saved in this table
-----------------------------
| idProveedor | idFamilia |
-----------------------------
| 5 | 1 |
-----------------------------
| 5 | 2 |
-----------------------------
| 6 | 2 |
-----------------------------
After a Provider is created, it can be edited and the values selected for idFamilia can be changed. So I have three different scenarios of what could happen:
The numbers of rows remain the same and I would only need to edit the values of idFamilia.
Some items were deleted from the selection and I would need to search the values on the table and delete those rows.
New items are added and I would need to insert those additional values.
Right now I am thinking about just deleting all the rows with the idProveedor that is being edited and just insert the new selection.
Is this a good practice? Will this affect the performance on the long run?
Could you recommend a tutorial or example to do it the other way?
I always go the way to delete and re-create them.
While this adds some performance penalty, it keeps the code simple to understand and easy to maintain. Unless you're Uber, Google, Facebook, Amazon, or other world scale app, the benefits you'd get from optimizing this are much smaller than the cost it incurs: in development, debugging and maintenance time.

How to store daily hits for items using MySQL and PHP? [closed]

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 7 years ago.
Improve this question
In my MySQL database, I'm trying way of creating a table to track daily views on 100 items. Each day would insert a new row for each item. So the table columns would look like this:
| ItemID | ItemName | Hits |
Is there a way to store daily hits for items?
What you could do is have a table with a structure like:
| item_hits |
|---------------|
| itemID | date |
Then your SQL query could look like
SELECT count(*) FROM item_hits WHERE date = '2015/04/15' and itemID = 7;
You wouldn't need to save the itemName in the table, because you should be able to get the itemName by joining it to your items table.

PHP mysql table rows [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I want to make like following:
I have a page, where users can register..
And another page, with their profile..
mysql table look's like following:
USER
-----------------------------------------------
| id | name | age | about | registered | email|
-----------------------------------------------
INFO
--------------------------------------
| id | title | content | date | hits |
--------------------------------------
Informations about user are stored in USER database..
Now user want to add some "INFO" in their accound everyday..
when user add their info, there will be another page wich will show:
NAME REGISTERED with EMAIL (from USER database) added following
TITLE (from INFO database)
CONTENT (from INFO database)
DATE (from INFO database)
HITS (from INFO database)
I really dont know how to do that ..
Pls understand me.. Im newbie on PHP
Cheers!
Try this
SELECT u.email,i.title,i.content,i.date,i.hits FROM user u,info i where u.id = i.id;
Try the mySQL JOIN command that will bring data from two or more data tables together via a specific collaboration.
I can't see where you link the USER table to the INFO table.
My suggestion is that you must create another table to be able to connect the two tables. Something like:
---------------------
| USER_ID | INFO_ID |
---------------------
| 1 | 1 |
---------------------
| 1 | 2 |
---------------------
That way, you could make a JOIN between the two tables.

how to use multi-keywords search with php and mysql? [closed]

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 9 years ago.
Improve this question
how to use multi-keywords search with php and mysql ?
I have a product table like this
the keywords field is save the keyword id
| id | name | keyword_ids |
|112 | apple | 123,12,421,121|
|113 | phone | 23,14,12,1 |
and the keyword table like this
|id | name |
|1 | white |
|2 | eat |
I want use a product keywords field find the similar product, how can I do it?
If you're set on using this data structure, you can use FIND_IN_SET
SELECT * FROM `products` p
LEFT JOIN `keyword` k ON FIND_IN_SET(k.`id`, p.`keyword_ids`)
WHERE k.`name` IN (?,?,?)
What I'd recommend doing is actually from a many-to-many relation table linking a product to keywords eg:
product_has_keyword
product_id | keyword_id
------------------------
112 | 123
112 | 12
112 | 421
112 | 121
That way you can use index for a join (which will be much faster)

Form show select box after choice option on select [closed]

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 9 years ago.
Improve this question
I intend that after selecting an option from a list, a new list appears according to the option selected.
But the second select list, sends a query to the database, depending on the choice list in the first select!
example:
table bd
| A | 1 |
| A | 2 |
| B | 3 |
| B | 4 |
first select
A or B
second select
if select A
1
2
if select B
3
4
A simple search for a filtered dropdown list brought me to the following code on GitHub which does a decent job explaining how to program one of these lists on your own. You can find it here.
Hope this helps.

Categories