There's a table that has three fields: id, word, username
And it currently has data like this:
1 | run | thisguy
2 | run | thisguy
3 | go | thatgirl
4 | go | thatguy
5 | go | thatgirl
The ones I want is 1, 3, and 4. I don't want 2, and 5 because they're duplicates.
How would I take them out?
Doing on the same table :
Create a unique index on the required fields/
Doing in new table :
a) Create a temporary table by selecting unique data from original table.
b) Drop original table.
c) rename temporary table to original table's name.
CREATE TABLE tempr SELECT id, type,name FROM myTable GROUP BY type,name;
DROP TABLE myTable;
ALTER TABLE tempr RENAME TO myTable ;
Also create unique index on type,name on this table to prevent future duplicates.
DELETE t2 FROM mytable AS t1
JOIN mytable AS t2 USING (word, username)
WHERE t1.id < t2.id;
You can use mysqls build in function distinct or GROUP BY
Distinct
Mysql_query("SELECT DISTINCT id, word, username FROM persons");
Group by
Mysql_query("SELECT * FROM persons GROUP BY word");
of course you can group by username or whatever you would like too.
Jonas
if you want to have only unique records in your table then do what DhruvPathak has said in his answer. But if you want to get the records through query then you can use distinct keyword. Your query will look like this
select id,distinct(word),username from $table
Have you tried using DISTINCT clause?
Related
I have the below data entered into a database from a website:
USER | SCORE
abc | 4
ghi | 2
abc | 6
def | 1
abc | 3
I want to now display a list of all the users, in alphabetical order, without repeating itself, and their total score. So with the table above, it would look something like:
abc: 13
def: 1
ghi: 2
How do I do that? I would use php, right? Or javascript?
User the Query to fetch data from database
SELECT USER,
SUM (SCORE) as SCORE
FROM table
GROUP BY USER ORDER BY USER ASC;
You should use PHP to fetch data from DB and print.
Your query will be something like:
select user, sum(score) as total_score from table group by user order by user asc;
It can be done in MySQL without involving any other programming language.
The following should do the trick using MySql query:
SELECT USER , SUM(SCORE) FROM table GROUP BY USER ORDER BY USER ASC
You can do this in database itself using the below SQL query.
SELECT SUM( SCORE ) , USER
FROM SampleTable
GROUP BY USER
ORDER BY USER
Try this:
SELECT User, SUM(Score) as Total_Score
FROM TableName
GROUP BY User
ORDER BY User;
No need to use javascript or php to achieve it. SQL is powerful enough to achieve it alone.
You need to fetch data from table and iterate through it. Below is SQL:
SELECT User, SUM(Score) as total
FROM TABLE_NAME
GROUP BY User
ORDER BY User;
I have some doubt. Actually i need to fetch data as per column value by joining two table using PHP and MySQL .I am explaining my tables below.
db_specials:
id special type
1 Food and Beverage 1
2 Liquor 2
db_basic:
id special_id name
1 2 aaa
2 1 Raj
3 2 Ram
4 2 Jay
5 1 Nikesh
Here i need to fetch all data from the tables db_basic those are associated with Liquor from first table. I am explaining my code below.
$res=mysqli_query($connect,"select b.id,b.special_id,b.name,s.type from db_basic as b inner join db_specials as s on b.special_id=s.id where b.special_id=2 order by b.id desc");
while($row=mysqli_fetch_array($res)){
$data[]=$row;
}
I am getting also the proper data. Here problem is db_specials also has the delete functionality on front end with insert. Suppose user deleted the row which has id=2 in db_specials table and insert again,in this case the id will change 2 to 3. So the query also needs to change accordingly. Here i need to know what should be the best logic so that each time user will not change the query if any specials is deleted and inserted again. Please help me.
Instead of id column, you should rely on the type column of db_specials table, since it's not going to be auto incremented for each INSERT operation. So your query should be like this:
select b.id,b.special_id,b.name,s.type
from db_basic as b
inner join db_specials as s
on b.special_id=s.type
where b.special_id=2
order by b.id desc
Also, I recommend that you should change the special_id column name of db_basic table to type, like this:
+------+------+------+
| id | type | name |
+------+------+------+
| | | |
This way, it would be easier for you to construct the query, like this:
select b.id,b.type,b.name
from db_basic as b
inner join db_specials as s
on b.type=s.type
where b.type=2
order by b.id desc
if user deleted 2,Liquor,2 in db_specials and reinsert, you cannot control what the user input in the front end.
User might be insert Food,2
Instead of join the type column of db_specials with db_basic, you should put a checking in the front end, when user click the delete button, prompt a message before delete 2,Liquor,2 from the table db_specials, if there is any special_id=2 in table db_basic the corresponding rows will deleted as well.
Hi i am not sure how to put this in a brief sentences, but i have DB table like the following
User Table
user_id
username
and so on...
Item
item_id
item_name
Item_Equipped
equipped_id head (FK to item_id)
hand (FK to item_id)
user_id (FK to user_id IN User Table)
I would like to generate a query that will display like the following format
user_id | head | head_item_name | hand | hand_item_name | ...
So far i only able to do this:
SELECT user.user_id, user.username,
equipments.head, equipments.head_acc,
equipments.hand,
equipments.acc, equipments.body
FROM gw_member_equipped AS equipments
LEFT JOIN gw_member AS user ON user.memberid = equipments.member_id
Which (i have to be brutally honest) doesn't do anything much.
I tried to perform INNER JOIN between item and item_equipped however i am unable to get individual name for each item (based on its item ID)
you need to join ITEM table two times with ITEM_EQUIPPED table.
you can use below query for your desired output column shown in question..
SELECT USER.User_Id,
Item_Equipped.Head,
Item_Heads.Item_Id Head_Item_Id, -- if you want you can remove this column
Item_Heads.Item_Name Head_Item_Name,
Item_Equipped.Hand,
Item_Hands.Item_Id Hand_Item_Id, -- and this column also as these columns are same as their previous select columns
Item_Hands.Item_Name Hand_Item_Name
FROM USER, Item Item_Heads, Item Item_Hands, Item_Equipped
WHERE USER.User_Id = Item_Equipped.User_Id
AND Item_Heads.Item_Id = Item_Equipped.Head
AND Item_Hands.Item_Id = Item_Equipped.Hand
I have 2 tables
Table name Dist.
NAME|Phone|ID
----------------
Oakley|555-555|1
Maui|666-666|2
lux|777-7777|3
Table name Patientacct.
Name|prescription|id|orderfrom
-------------------------------
bob|20-20|1|oakley
billy|15-20|2|Oakley, Maui
kim|20-20|3|Lux
I'm looking for a display like
Oakley
--------------------
Bob
Billy
Maui
------------------
Billy
Lux
--------------
kim
Trials so far
SELECT *
FROM `dist`
JOIN `patientacct` ON patientacct.dist LIKE CONCAT('%', `dist`.name ,'%')
GROUP BY `dist`.name
This showed only 1 dist
If I drop the group by example:
SELECT *
FROM `dist`
JOIN `patientacct` ON patientacct.dist LIKE CONCAT('%', `dist`.name ,'%')
I get the record twice so what I need is somewhere between the two. I'm extremely new to joins so be easy when explaining.
you must change your tables structure and you need to add a relation table with this values:
orders:
dist_id, patientacct_id PRIMARY_KEY(dist_id, patientacct_id)
First of all, please make the table structure relational. Have a primary auto_increment value in each table and use the primary key from the Dlist foreign key to Patientacct. This way, a simple join would fetch the record you have wanted.
Regarding the display, what do you mean you don't want record twice. Since you are using join, you will get two records for Oakley with the patient Bob and Billy. That's what you wanted and it would be like that by a simple join too in a relational table.
How can I run a select query on a multivalue attribute? Does mysql have a function do select certain data from a multivalue field? Much help is appreciated. Here's a sample of the problem:
Table
userid groups
-------------------
2 2,3,5
4 1
9 2,5,10
datatype is char(250) for groups
I want to do a query to select all userids that belong to group 5, in this example it would be userid 2 and 9. So:
userid
------
2
9
Any way to go about it with a mysql query? or with php/mysql?
In case the groups datatype is SET
You can use
SELECT * FROM users WHERE FIND_IN_SET('5', groups);
UPDATE
In case of char or varchar. You can use this
SELECT * FROM users
WHERE
groups LIKE '5,%'
OR groups LIKE '%,5'
OR groups LIKE '%,5,%'
OR groups = '5'
This is ugly, but if you want to do it completely in MySQL, you could do:
SELECT *
FROM users
WHERE groups LIKE '%,5,%'
OR groups = '5'
OR groups LIKE '%,5'
OR groups LIKE '5,%'
You'd be better off having a separate table with one group per row and a reference back to the user. You could do a join with a much simpler condition with that schema.
SELECT users.*
FROM users
INNER JOIN users_groups ON (users.user_id = users_groups.group_id)
WHERE users_groups.group_id = 5