get duplicated fields and these fields may have spaces in mysql database - mysql

I have this mysql table
id | phone | name
1 | 123456| aaaa
2 | 454535| bbbb
3 | 123456| cccc
4 | 123456 | ddd
based on above data these records:
1 | 123456| aaaa
3 | 123456| cccc
4 | 123456 | ddd
are duplicated but the first one is trimmed and second has space at the beginning and the third has space at the end.
I have written this query:
SELECT phone, count(trim(phone)) FROM users GROUP BY trim(phone) HAVING count(trim(phone)) > 1
but did not return any records.

May be your problem with Check for line break or carriage return. No problem with white space.
SELECT * FROM users WHERE phone REGEXP "\r\n";

Here is a slightly cleaned up version fo your query.
SELECT TRIM(phone), count(id) AS Counter
FROM users
GROUP BY TRIM(phone)
HAVING Counter > 1
This query returns the following result:
phone | Counter
123456 | 3

Related

MySQL ORDER By one Specific value in column (having comma separated values)

I want to sort the user record according to city (chosen from the drop-down list). like if I pass city_id 22 in my query then i want all the row first which are having city_ids 22 then the rest of the rows.
I know WHERE find_in_set('22',city_ids) will give me the correct result but it will not return the all rows so I want to achieve it using some ORDER BY .
I have tried ORDER BY FIND_IN_SET('22',city_ids) but its not working. How do I fix this, any best way?
User Table:
Id Name city_ids
1 AAAAA 10,22,30
2 BBBBB 11,28
3 CCCCC 15,22,44
4 DDDDD 19,99,
5 EEEEE 55,27,22
Want Sorted Output like below:
Id Name city_ids
1 AAAAA 10,22,30
3 CCCCC 15,22,44
5 EEEEE 55,27,22
2 BBBBB 11,28
4 DDDDD 19,99,
You can do:
ORDER BY (FIND_IN_SET('22', city_ids) > 0) DESC
This puts matches first.
Then you should fix your data model. It is broken, broken, broken. Storing lists of ids in a string is wrong for many reasons:
The data types are (presumably) wrong. The ids are numbers and should not be stored as strings.
Storing multiple values in a column is not the SQL way to store things.
Ids should have properly declared foreign key relationships, which you cannot declare.
SQL does not have very good functions for processing strings.
The resulting queries cannot take advantage of indexes or partitioning, impeding performance.
SQL has this really great data structure for storing lists of things. It is called a table, not a string column.
The expression:
FIND_IN_SET('22', city_ids) > 0
will return 1 for all rows where '22' exists in column city_ids and 0 for the others.
So, after that you need add one more level for sorting by id ascending:
ORDER BY
FIND_IN_SET('22', city_ids) > 0 DESC,
id
See the demo.
Results:
| Id | Name | city_ids |
| --- | ----- | -------- |
| 1 | AAAAA | 10,22,30 |
| 3 | CCCCC | 15,22,44 |
| 5 | EEEEE | 55,27,22 |
| 2 | BBBBB | 11,28 |
| 4 | DDDDD | 19,99 |

Need help on SQL query - Select data with duplicate multiple data entries in one data field

I need to select all data having non-duplicate IDs..
here's my sample table..
----------------------------------------------------------------------------------
ID | Zip-Code | Search Query | ID_LIST
----------------------------------------------------------------------------------
1 | 1000 | Query Sample 1 | 13,14,15,
----------------------------------------------------------------------------------
2 | 2000 | Query Sample 2 | 16,13,17,
----------------------------------------------------------------------------------
3 | 3000 | Query Sample 3 | 18,17,13,
----------------------------------------------------------------------------------
4 | 4000 | Query Sample 4 | 15,16,17,18,
----------------------------------------------------------------------------------
5 | 5000 | Query Sample 5 | 19, 20,
u can notice that IDs 1 and 2 have duplicate, which is 13 on ID_LIST
2 and 3 also have duplicate, which is 13 and 17.
What I want to do is make it like this...
----------------------------------------------------------------------------------
ID | Zip-Code | Search Query | ID_LIST
----------------------------------------------------------------------------------
1 | 1000 | Query Sample 1 | 13,14,15,
----------------------------------------------------------------------------------
2 | 2000 | Query Sample 2 | 16,17,
----------------------------------------------------------------------------------
3 | 3000 | Query Sample 3 | 18,
----------------------------------------------------------------------------------
5 | 5000 | Query Sample 5 | 19,20,
What query would be good for this? Any Help?
Best way to approach it is to normalize your data, as mentioned in comments. But if you absolutely have to do it this way, it would be very difficult to do in query on mysql.
I would suggest you to create a procedure for it. As and when you develop each step, you can google that particular solution of that step, and test it and build up on that. Let me know if any step sound confusing/unclear.
Create a variable string, say v_vals. Initialize with null. At the end of procedure, it will contain all the distinct values of id_list (13,14...20)
Iterate through each row.
Count the number of comma in id_list.
Loop from 1 to number of comma.
In every iteration, use substring and instring to find position of each comma and then extract values from id_list. (13,14...)
use another variable v_id_list. Put null in it.
Search for the values (from step 5) in v_vals. If they exist in v_val, then skip them, else put them in v_val and v_id_list.
Now run an update statement to update id_list with v_id_list.
Now repeat Step 3 to 8 for each row.
Note that v_id_list will be reinitialize for each loop, however v_val will contain all the distinct values of id_list.

rate difference one word with field of table

Suppose you have a table
id | title
-----------
1 | ali
2 | Ali
3 | alireza
Now I want you to say a command word "ali" check the field's title.
And the output of the table was a table that all records should be returned in the table.
Now the results should be like.
id | title | diff
---------------------
1 | ali | 100
2 | Ali | 97
3 | alireza | 50
Important: only use commands Sql
Here you go:
select id, title,
case title
when 'ali' then 100
when 'Ali' then 97
when 'alireza' then 50
end case diff
from mytable;

MYSQL - order from table by X starting with id Y

I my mysql db I have a table with 3 parameters ( name, views, id ). I need to get row ordered by views. I'm getting something like this.
query:
select
from table
order by views
Result:
id | name | views
------------------------
7 | xxxx | 9000
2 | yyyy | 8000
1 | aaaa | 7000
4 | bbbb | 6000
8 | dddd | 5000
6 | cccc | 4000
5 | oooo | 3000
3 | tttt | 2000
What I need to do, is to get rows ordered by views but starting with specyfic ID. Is it possible. All input that i have is ID. Let sat that ID is 6, this should be output:
id | name | views
------------------------
6 | cccc | 4000
5 | oooo | 3000
3 | tttt | 2000
I can't use LIMIT as I don't really know what is possition at the moment. I just need to get rows which are left starting with ID.
What I'm trying to do is to get infinite scroll, I requesting next elements base on last element that was displayed. Only tricky part is that I'm ordering by views parameter.
select * from table
where (views = 4000 and id>6) or (views < 4000)
order by views desc, id asc;
The tricky part is that you have to know (select) the views of the element with ID 6; also you need to use the ID as secondary sort criteria in order to get consistent results.
Actually this is a common case of a since,until type of paging
SELECT * FROM table
WHERE views <= (SELECT views FROM table WHERE id = 6)
ORDER BY views

MySQL - COUNT before INSERT in one query

Hey all, I am looking for a way to query my database table only once in order to add an item and also to check what last item count was so that i can use the next number.
strSQL = "SELECT * FROM productr"
After that code above, i add a few product values to a record like so:
ID | Product | Price | Description | Qty | DateSold | gcCode
--------------------------------------------------------------------------
5 | The Name 1 | 5.22 | Description 1 | 2 | 09/15/10 | na
6 | The Name 2 | 15.55 | Description 2 | 1 | 09/15/10 | 05648755
7 | The Name 3 | 1.10 | Description 3 | 1 | 09/15/10 | na
8 | The Name 4 | 0.24 | Description 4 | 21 | 09/15/10 | 658140
i need to count how many times it sees gcCode <> 'na' so that i can add a 1 so it will be unique. Currently i do not know how to do this without opening another database inside this one and doing something like this:
strSQL2 = "SELECT COUNT(gcCode) as gcCount FROM productr WHERE gcCode <> 'na'
But like i said above, i do not want to have to open another database query just to get a count.
Any help would be great! Thanks! :o)
There's no need to do everything in one query. If you're using InnoDB as a storage engine, you could wrap your COUNT query and your INSERT command in a single transaction to guarantee atomicity.
In addition, you should probably use NULL instead of na for fields with unknown or missing values.
They're two queries; one is a subset of the other which means getting what you want in a single query will be a hack I don't recommend:
SELECT p.*,
(SELECT COUNT(*)
FROM PRODUCTR
WHERE gccode != 'na') AS gcCount
FROM PRODUCTR p
This will return all the rows, as it did previously. But it will include an additional column, repeating the gcCount value for every row returned. It works, but it's redundant data...