how print unique values in 2 columns using php [closed] - php

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 5 years ago.
Improve this question
two columns
user_id purchase_item
1 watch
1 TV
2 watch
2 fridge
2 Toys
when i use distinct comand in sql
same as it is show data but i want to following
user_id:1
purchase_item:watch
tv
user_id:2
purchase_item:watch
fridge
Toys
means i want user id print only one time so how it is possible in php
plz help me and tell coding please

If you want to do it completely in Mysql you can use aggregation to get items as comma separated list per user
select user_id, group_concat(purchase_item) purchase_item
from table
group by user_id
this will give you results like
user_id purchase_item
1 watch,TV
2 watch,fridge,Toys
Also note using group_concat the result is truncated to the maximum length that is given by the group_concat_max_len system variable, which has a default value of 1024. The value can be set higher, although the effective maximum length of the return value is constrained by the value of max_allowed_packet
But its better if you do this in php just get the ordered results and apply your logic to show user_id only once for multiple purchase_item related to that user
select * from table order by user_id asc

Related

Random like order with mysql and PHP [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 6 years ago.
Improve this question
I use PHP and Mysql. I have a table that looks kind of like this:
id title
----------
1 my title
2 another title
3 The last title
Now I want to select them with a random like order.
I will need to use LIMIT because of the query size.
The random like order should always be the same random order every time.
Example result, every time
3 The last title
1 my title
2 another title
Do another query run:
3 The last title
1 my title
2 another title
The same random like result appear.
Possible solutions
Add a real random number stored as a new column generated by insert.
Some fancy SELECT query that does some magic.
Something else?
Why I want this is that I insert products, first from one site, then from another. In the result I want to present them as a mix.
It's not really random at all, but then again your request wouldn't work with random numbers. You want some sort of a hash of each record to use in the order by. You can probably find something better, but as a simple example you could use:
SELECT * FROM my_table ORDER BY MOD(id, 2);
Which you can see working here:
http://sqlfiddle.com/#!9/269a4/1/0
Use ORDER BY RAND().So your query should be :
select * from <tablename> where <cond> then ORDER BY RAND() limit <startlimit>, <end limit>

Group by and show specific row value [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 6 years ago.
Improve this question
I have this code to output the value in table i used group by to
group the same item code but serial does not show it only show
onevalue the 123321. The output should be 123321,4354
$invoicequery=mysqli_query($link,"Select SUM(INVOICE_ITEM_QTY_MX),INVOICE_ID_MX,INVOICE_ITEM_UPRICE_MX,INVOICE_ITEM_AMOUNT_MX,INVOICE_ITEM_SERIAL_MX,INVOICE_ITEM_DESC_MX,INVOICE_ITEM_CODE_MX from invoice WHERE BRANCH_CODE_MX='".$display_branchcode."'
and INVOICE_NO_MX='".$invoicecode."' GROUP BY INVOICE_ITEM_CODE_MX");
while($row=mysqli_fetch_array($invoicequery))
{
$invoiceid=$row["INVOICE_ID_MX"];
$itemcode=$row["INVOICE_ITEM_CODE_MX"];
$itemquantity=$row['SUM(INVOICE_ITEM_QTY_MX)'];
$unitprice=$row["INVOICE_ITEM_UPRICE_MX"];
$amount=$row["INVOICE_ITEM_AMOUNT_MX"];
$serialimei=$row["INVOICE_ITEM_SERIAL_MX"];
$itemdescription=$row["INVOICE_ITEM_DESC_MX"];
}
Ouput
In Database
It sounds like you are looking for MYSQL's GROUP_CONCAT() function method. Simply wrap the column name you wish to have returned as a comma separated list with that function like you have used MAX().
Example:
SELECT student_name,
GROUP_CONCAT(test_score)
FROM student
GROUP BY student_name;
This will return, for example,
student_name | test_score
Becky C. | 80, 77, 95
Note that this value will be truncated according to your db's group_concat_max_len setting.

How can I Select duplicate results, insert the results to a new table and delete them from the original table? (phpmyadmin) [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 6 years ago.
Improve this question
I have 2 tables, both with the same column names that look like this
I have an app that is used for clocking in/out. A user goes up to a tablet and selects there name, the data is then inputted into table 'clocktable' as shown above.
What I need to happen is when a user presses their name for a second time (to clock out) it detects that there is already a value in the table with the same employeename value, and moves both clocks to a different table 'oldclocks' so that a record is kept of clocking in/out times. The reason it needs to be in a separate table is I have a web page that displays the 'clocktable' table so we can determine who is in the building.
How can I go about doing this?
I can execute
SELECT DISTINCT EmployeeName, time FROM clocktable ORDER BY EmployeeName
from within myphp and then execute
INSERT INTO oldclocks SELECT Employeename, Department, time FROM clocktable;
Afterwards, but I cannot seem to execute them both at the same time in order to get them into a php script. Also is there a way I can delete the select results from the 'clocktable' at the same time?
Help with this would be greatly appreciated.
You can add an indicator, where 1 is in and 0 is out & if you want to save the logs, then creat another table to INSERT a record whenever the indicator's value change.
1- add 2 new attributes in table A, lets say userid & in_out for an example.
2- in_out = 1/0 where 1 is in and 0 is out
3- creat table B and insert userid & time every time the in_out value change.
And that's how you know if the user in the building or out, also you will keep logs of every move
2 tables are not nessesary. And it is bad idea.
Why dont you simply add boolean IN_BUILDING which will have 0 ->Not in building and 1->In building. Then you can do:
SELECT EmployeeName FROM clocktable WHERE IN_BUILDING = 0; which will show you all employees who are not in building
or
SELECT EmployeeName FROM clocktable WHERE IN_BUILDING = 1; which will show you all employees who are in building that moment.
Its easier also because you can change it value easy on click.

How to echo fetched array data from a mysql database in custom order? [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 8 years ago.
Improve this question
I have been trying to print out data from a database table in custom order like for example..
I have a table and that have alot rows and have one column as listing_type which have values like Gold,Premium,Silver,Free etc for each row..! so how I would be able to print the data from fetched array in order like at first it should echo all Gold and then Premium and then Silver and then Free etc like..!
Any help would be appreciated..Thanks waiting for your reply.!
Use an ORDER BY clause in your MySQL query like this:
SELECT *
FROM `table`
ORDER BY FIELD(`listing_type`, 'Gold', 'Premium', 'Silver', 'Free')
The MySQL function FIELD() returns the position of str in the given strings. With this, you can create a custom order to sort your results on.
In Mysql query you can easily order results by using field() function,so returned results will be ordered in way where listing_type is gold first then premium then silver results then free
select *
from your_table
order by field(listing_type,'Gold','premium','Silver','Free' )

MySQL PHP Select and Edit Values From Database [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 9 years ago.
Improve this question
I have a database containing a list of characters for a game. The user is required to search for 5 characters and assign a position to each selected character. This list of 5 with positions needs to be saved so that it can be retrieved later.
In VB.NET I simply created a file that contained the selected 5 with their assigned positions and read the data from this.
Now I am trying to create a web-based version I am struggling as to how I should save the data. The large database is in a MySQL table. I am guessing that it wouldn't make sense to create a new table for each user and have individual rows for their selected characters? My thinking is to have a single table called 'users' then store the selected characters in a column with their position e.g. "player1:pos4,player56:pos302,player322:pos392' etc. then I can split the values later.
Does anyone have any more efficient ideas on how to approach this? I am very new to all this thanks!
I would create 3 basic tables: users, characters, positions, and 1 lookup table: usercharpos with 3 columns: user_id, char_id, pos_id. Each user, character and position should have a unique id as primary key. Then, in the usercharpos lookup table I would associate a user with the 5 chosen characters and the position of each one. For example, we have a user with id = 5, that chose some characters with ids = 1,2,3,4,5 and chose some positions for them with ids = 2,3,6,7,8. The usercharpos would become:
user_id char_id pos_id
5 1 2
5 2 3
5 3 6
5 4 7
5 5 8
That way, you keep your database normalized and each entity has its own table without multiple values in a field.

Categories