How to retrieve duplicated data from database in PHP - php

I have in my database a table that Continent users ,the first field is NAME and this name can be duplicated .
I want to retrieve user's name . for example if i have in my table the name John 3 time and David 2 time and Katy 1 time,
I want the result to be john David Katy ,
If the name are duplicated i want to escape it ,and display just names once for every name, i want to do this using PHP and MYSQLI .

If you have a column with many duplicate values and only want to list the unique values you can use the DISTINCT keyword to only return those,
SELECT DISTINCT `NAME` FROM `TABLE_NAME`

Related

How to retrieve data from table which is separated by comma in sql?

I have table students containing data like given below :
id name activity
1 susan 1,2
2 denny 1,2,3
3 ken 3
I want to choose all the data like name and id from this table having activity equal to 2. I have done a sql query using like but it will not work.
$sqll = mysqli_query($con,"SELECT id,studentname FROM students WHERE activity LIKE '%$activity'");
If another row contains data like id : 4 name:ben activity : 22 it will show this row also. I want the rows containing activity 2 only.
Can anyone suggest a solution for this ?
I agree with the comments so far about normalising the data but to answer your question with the table structure as it is you can do this:
$sqll = mysqli_query($con,"SELECT id,studentname FROM students WHERE CONCAT(',',activity,',') LIKE '%,$activity,%'");
Note, you concatenate a comma either side of the field data and the specific activity you are looking for. Also note, I added a % on the right of the like as well so that "denny" will be caught
SELECT id, studentname
FROM students
WHERE FIND_IN_SET("$activity", activity);

update column based on two columns

I have a weird problem.
I have a rather large database with two tables. I need to change a column's contents from a name to an ID that already exists in another table.
Example:
I have a table that contains a column "Name"
the name column has the persons "lastname, firstname" as shown
Name | othercolumn
Smith, John |
I would like to change the contents of the name column to the staffID associated with the persons name.
The staff table is
staffID | firstName | lastName
1 john smith
My end result should be
Name | othercolumn
1 |
I've tried all sorts of joins and concats, but can't seem to get it down with my limited mysql knowledge. Is there a way to do this without having to do it manually? The comma seems to give me alot of grief. Thanks!
You need to be very careful about this. First, I assume that StaffId is a number. So, add a column to the table:
alter table t add StaffId int;
Then, update this column:
update t join
staff s
on t.name = concat_ws(',', s.lastname, s.firstname)
set t.StaffId = s.StaffId;
Note that after you have done this, you may still have StaffId values that are NULL:
select t.*
from t
where t.StaffId is null;
These are the names that are not in the staff table. They require more work. When you are done, you can drop the name column.

how to display all row data of mysql field name

I am trying to display all the data of mysql field but having issue display it.
What I want to do is display all the first name and last name of the members which has a membership level of 1 so if there are 20 users with membership level 1, it would display 20 first name
$query = "SELECT first_name, last_name FROM " . $wpdb->prefix . "ss_table WHERE membership_level = 1";
echo $query[20];
Your sql query is wrong, you should first check out sql sites for writing query
For fetching a single column from a table you have to use
select first_name from ss_table
but for fetching more than one column you have to append "," after every column name except after the last column
select first_name, last_name from ss_table
You've written a query, but you need to execute it if you want to get results. Echoing $query[20] will just give you the 20th character of your query statement. Look into some docs: http://php.net/manual/en/book.mysqli.php or tutorials on how to connect to a database.

PHP And MySQL Search Based on two fields

Alright, so I understand how to do a mysql query to return results based on one search parameter. But I have a table that contains usernames and data. The data should be unique to each username, but I want to return any instances where that data might overlap over multiple usernames. An example would be
Username | Data Field
----------------------------------------------
Test | Abcd1234
Test2 | efgh5678
Test3 | Abcd1234
Test4 | efgh5678
I want my php script to return all instances where duplicate entries are found in the data field. Note that, neither the data field nor username field are unique in this table. The table gets populated whenever the user completes an action, so there should be many entries for each username, but each time they should have the same data field. I only want to check when two different usernames have the same data field. Does anyone have any idea how this can be done in php or a mysql select statement? It may take more than one query and that is okay. I've tried searching on how to find duplicate emails based on usernames, but the results I found were more on preventing duplicate registrations in the first place.
Basically, you want to count the number of unique users for each data field. This is an aggregation query:
select data, count(distinct username) as numusers
from table t
group by data;
You should be able to get the unique user names with this query:
select data, count(distinct username) as numusers,
group_concat(distinct username) as users
from table t
group by data
having count(distinct username) > 1;
This will create a comma separated list of users "using" the same data.

how to select rows from mysql database according to multiple specification

I have an array, $username which contains a a bunch of username, like(lets make it simple to list 4 values)
$username[0]='nicholas', $username[1]='Jane', $username[2]='Frank',$username[3]='Ben'
I have a table in database like
id username post timestamp
1 nicholas hello 13932...
2 Jane world 13922...
3 kesong yes 13978...
4 Frank bad 13374...
5 Sim tii 13842...
6 Ben tisue 13149...
I want to select the rows which only match my values on the array,$username, and order by timestamp
Instead of:
//list all the value
$query="SELECT FORM `table1` WHERE `username`='nicholas' || `username`='Jane' ORDER BY timestamp "
any convenient way to fulfill this job because the array always change its value
Probably the best method is to construct you query with an IN clause.
https://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in
If all your searchable names are contained in $username, you could create a dynamic query, like so:
"SELECT FORM table1 WHERE username IN ('".implode("','", $username)."') ORDER BY timestamp"

Categories