how to get the exact row using query - mysql

I want to get the exact row from the following data
id name groupid
1 robert 1,2
2 henry 11,12
My query is
SELECT * FROM table WHERE groupid LIKE '%1%'
Above query will return both row
How to get the first row ?

You could use FIND_IN_SET
SELECT * FROM table WHERE FIND_IN_SET('1', groupid)
But as suggestion, you should not save data like this.

assuming groupid is a varchar column having ids stored as comma seperated list you can try this:
select * from table where CONCAT(',',groupid,',') LIKE '%,1,%';
or better approach would be to use FIND_IN_SET function in mysql:
select * from table where find_in_set(1, groupid);

To get an exact row from a database table, just specify all the fields:
SELECT *
FROM myTable
WHERE id = 1
AND name = 'robert'
AND groupid = '1,2'
Or, assuming id is the unique primary key, you can just use that:
SELECT *
FROM myTable
WHERE id = 1

Using LIKE '%1%' will return all the results which contain 1. Use #lc solution WHERE groupid = '1,2' to get only result with that specific id.

Related

Mysql querying with id and last_name yields 0 results

In mySql When I search with id and last name I see no results. Below
SELECT * FROM TableName
WHERE id = '-2105192515' AND last_name='JOHNSON'
0 Results ----- (id = mzb_indiv_id)
But when searching with only Id I get one record match. Below is the query
SELECT * FROM TableName
WHERE id = '-2105192515'
1 Record match . --- (id = mzb_indiv_id)
Also when searching with last_name I see the results too. But why is there a difference between the above two searches?
The first query only returns a row if it has that id and also has that last_name, in the same row.
(It does not mean : rows with this id and also rows with that last name.)
I guess the result from your second query doesn't have the last_name JOHNSON?
Perhaps you want an OR rather than an AND?
Edit
Your example data has ' JOHNSON' rather than 'JOHNSON' (an extra space at the start)
Try last_name LIKE '%JOHNSON%' or TRIM(last_name) = 'JOHNSON'
From your question, first thing I found
SELECT * FROM TableName
WHERE id = '-2105192515'
AND last_name='JOHNSON'
Here column name is 'last_name' but in in info below you wrote column name 'last_Name'.
Check your column names, as the missing column would give error in executing, so it is highly unlikely.
Second thing: are you spelling 'JOHNSON' the same way it is stored in your database? no spaces included?.
Please, post the result set with both queries to get a proper, and more valid answer.
The row which id is -2105192515 has a last_name column value which is not equal to JOHNSON
SELECT * FROM TableName
WHERE id = '-2105192515' AND last_name='JOHNSON'
This query means - return all the rows that their id = '-2105192515' AND the last_name='JOHNSON'
You may have wanted to use OR instead?

Put random values in table from values in a set

I want to put random values into my user-table with data from a set.
I got a list of firstnames and surnames and i want to replace all name columns in my table. I was thinking of using a set
SET #firstNames = 'Thomas,Chris,Sophia,Ava';
SET #surNames = 'Peterson,Bolander,Travolta,Anniston';
Is it possible to write a query which randomly updates values in the name column on each user?
Table
User table
---------------------
id
name
password
created_at
I just found a way doing it
UPDATE persons
SET first_name = (
SELECT val FROM (
SELECT '' as val
UNION ALL SELECT 'Thomas' as val
UNION ALL SELECT 'Chris' as val
UNION ALL SELECT 'Sophia' as val
......
) AS vals ORDER BY RAND() LIMIT 1
)
would not be good idea to do that with Mysql, try doing that with your front-end Language. in Mysql we have RAND() function but that returns random number between 0 and 1, it would be very long way to get your desired output with this, so tryout passing random names from your front-end language, which is very easy task

How to avoid overwrite in query results when a where condition like: "in (23,24,22,23)"

I have one such sql:
select name from A where id in (23,24,22,23)
When I run it in Navicat, the result only have one result of 23.
My question is, how to keep the number and order of the query results remains the same as (23,24,22,23).
If you want to maintain the order of the result then use order by clause like
select name from A
where id in (23,24,22)
order by id;
Again, assuming that id is a primary key column in your table A then there will be only one row with id = 23. How do you expect the same row to get repeated automatically unless you make it explicit by using a UNION ALL
If you really really want to fetch the records like this, you can use field function to get 23,24,22 and order by this sort:
select name from A where id in (23,24,22) order by field(id, '23,24,22')
then use union all get another 23:
(select name from A where id in (23,24,22) order by field(id, '23,24,22'))
union all
select name from A where id = 23

Concat 2 columns in a string, then get a count for each concatenation

I am trying to concatenate 2 columns, then count the number of rows i.e. the total number of times the merged column string exists, but I don't know if it is possible. e.g:
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(merged_columns)
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
Note: the colon I've inserted as a part of the string, so my result is something like 12:3. The 'count' then should tell me the number of rows that exist where column_1 =12 and column_2 = 3.
Obviously, it tells me 'merged_columns' isn't a column as it's just an alias for my CONCAT. But is this possible and if so, how?
Old question I know, but the following should work without a temp table (unless I am missing something):
SELECT
CONCAT(column_1,':',column_2 ) as merged_columns,
COUNT(CONCAT(column_1,':',column_2 ))
FROM
table
GROUP BY 1
ORDER BY merged_columns DESC
You can try creating a temp table from your concatenation select and then query that:
SELECT CONCAT(column_1,':',column_2 ) AS mergedColumns
INTO #temp
FROM table
SELECT COUNT(1) AS NumberOfRows,
mergedColumns
FROM #temp
GROUP BY mergedColumns
Hope this answer is what your are looking for.
Try this
SELECT
CONCAT(column_1,column_2 ) as merged_columns,
COUNT(*)
FROM
table
GROUP BY merged_columns
ORDER BY merged_columns DESC

Mysql only do not select duplicated values

I am looking for a way to run a simple SELECT statment. I have a table which has two columns: id and email.
I want to run a SELECT statment that won't return duplicate values. For example, take the following data:
1 example#hotmail.com
2 example12#hotmail.com
3 example#hotmail.com
4 example#hotmail.com
I want it to return only the following:
1 example#hotmail.com
2 example12#hotmail.com
...and skip the duplicate values.
SELECT MIN(id), email FROM some_table GROUP BY email
SELECT DISTINCT email FROM table
If you don't need ID use
SELECT DISTINCT email FROM `TABLE_NAME`
else If you need the First ID use
SELECT MIN(ID),email FROM `TABLE_NAME` GROUP BY email
There are several ways to accomplish this, one is to use the DISTINCT clause:
SELECT DISTINCT email FROM your_table;
another way is to summarize counts of the values:
SELECT COUNT (*), email from your_table GROUP BY email;
SELECT DISTINCT UNIQUE_FEILD_NAME FROM YOUR_TABLE_NAME