mysql query to search multiple values in comma separated list - mysql

I have a table Orders:
Order VARCHAR(20)
Developer VARCHAR(50)
I need to SELECT field in the following way:
For example I have this data in my Table:
Order Developer
Order 141 Tim (Apple), Alex (Microsoft), Sara (Amazon), Neylo
Order 171 James (Apple), John (Amazon)
Order 181 Nike (Microsoft)
Need to make SQL query to get this:
Developer Order
Tim, James (Apple) Order 141, Order 171
Alex, Nike (Microsoft) Order 141, Order 181
Sara, John (Amazon) Order 141, Order 171
Neylo Order 141
Is this possible to make?
Any idea is welcome

The best way would be to use a new table to make it atomic like others mentioned, but it sounds like you can't do that.
If you can add a stored function this solution will work.
http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
If you cannot add a function (e.g. hosted with no admin acces, etc). You could break the data apart using substrings (ugly ugly ugly -- I even hate to mention it it's so bad -- but it could work if you write for the maximum number of supported values).
https://stackoverflow.com/a/11008567/455627
e.g.
substring_index(`Developer`,',',1) ==> first value
substring_index(substring_index(`Developer`,',',-2),',',1) ==> second value
etc.

if this is your database then it is a mess. A golden rule is to have all data only one time in your database. Therefor you should create a table "user" that have all users in his:
user:
user_id
name
email
...
Then you could make your order table like this:
order:
order_id
user_id
...
Now you can get all information like this:
SELECT order.order_id, user.name FROM orders
JOIN users ON order.user_id=user.user_id
WHERE order.order_id=141
Understanding JOINS in MySQL

Related

How to group by with 2 different field for the below criteria

Name date sellingAmt
Raju 08-02-2017 2000
Ravi 09-02-2017 5000
Ravi 09-02-2017 2000
Raju 09-02-2107 1000
Expected Result
Name date sellingAmt
Raju 08-02-2017 2000
Ravi 09-02-2017 7000
Raju 09-02-2107 1000
Let me know how to group by this in mysql select query
Seems like a pretty basic question. What part are you struggling with?
Always post what you've tried so we can look at your train of thought and try to correct the problem at it's root instead of giving you a baked solution!
Date (and I just see name is as well) is a keyword/reserved word so out of habit I use the ` to escape them.
I used sellingAmt Desc as the order by since it appears you want those within the same date; though you could just as easily want name desc.
I group by name, date to achieve the desired results. While mySQL extends the group by so you can group on fewer values than what is in the select, I find this feature is wrongly used more often than correctly. So I tend to error on the side of caution and always group by all fields not part of an aggregate; which is what other RDBMS systems would require of you. In your case date and name are the unique combination to sum sellingAmt correctly and achieve your expected results.
.
SELECT `Name`, `Date`, Sum(SellingAmt) sellingAmt
FROM {tblName}
GROUP BY `Name`, `Date`
ORDER BY `date`, sellingAmt Desc
Replace {tblname} with your table name.

Find lowest value and coresponding code on asingle query mysql

I have a user table as follows
id name age
1 John 21
2. Mathan 23
3. Raj 21
4. Manoj 50
5 Krishnan 91
I want to find minimum age and its corresponding name. How can I do it with rails?
Can I do it in a single query?
Note: More than one names can have single age.
Is there a specific reason why you want to do it in a single query ?
If you can write 2 queries, I think you can just write :
User.where age: User.minimum(:age)
select age, group_concat(name) from table group by age order by age asc limit 1
You will need to process the results later on in ruby, but this gives all you need in one single query. Also i am assuming mysql, so might differ on other rdbms.
It gives exact output in mysql that you want try this
SELECT concat("[",name," ",age,"]") AS name
FROM TABLE
WHERE age =
(SELECT min(age)
FROM TABLE);

Use MySQL to Find Duplicates and Display on One Line

I'd like to use MySQL to find duplicates and then display those items as one, combined record. For example, I have two columns: Name and Fruit. A sampling of records might look like this:
Joe - Peaches
Faye - Bananas
Joe - Starfruit
Sam - Apples
I want to display this data as:
Joe - Peaches, Starfruit
Faye - Bananas
Sam - Apples
Is that possible? Could you help me with the MySQL query to start? I'm using VB.NET for my application. Thank you.
Use GROUP_CONCAT for that.
SELECT personName, GROUP_CONCAT(fruitName) fruitList
FROM tableName
GROUP BY personName
You'll want to use group_concat here.
SELECT name, group_concat(fruit)
FROM table
GROUP BY name

mysql combining records from one table

I have a single table that uses test# as the primary key. Here is what that table looks like:
Test# Name VerbalScore readingScore Notes
1 Bobby 92 Good job
2 Bobby 40 You Suck Bobby
The problem is I want to view and be able to see when there are multiple verbal scores for the same Name (so be able to see if the person took the same test more than once).
I want to have some kind of select statement to get this result from the above table:
1 Bobby 92 40 Good job, You Suck Bobby
Is that possible?
I am not totally sure I understand what you mean by "see when there are multiple verbal scores" but with mysql 5+, try
SELECT
Name,
GROUP_CONCAT(VerbalScore),
GROUP_CONCAT(readingScore),
GROUP_CONCAT(Notes)
FROM
myTable
GROUP BY
Name;
GROUP_CONCAT is a mysql specific grouping function.

how to search a word from varchar(comma separated) field in mysql?

i have a table
table_movie
mid muid actor_name movie_list(varchar)
18 act_6 tom hanks mov_18,mov_19,mov_2,mov_22,mov_23
21 act_9 jhonny depp mov_1,mov_10,mov_20,mov_22,mov_3,mov_9
28 act_16 bruce willis mov_18,mov_19,mov_2,mov_22,mov_23
29 act_19 jhon trovolta mov_1,mov_10,mov_20,mov_22,mov_3,mov_9
now i want to dispplay only those actor_name and muid which have mov_1( which comes from php) in their movie_list
if i use EXIST then it show error, my query is written below
`SELECT muid,actor_name FROM table_movie WHERE $movieID EXIST( movie_list)`
i also tried with RLIKE but no results!:(
please tell me how to search a single word from a varchar field
NOTE
my table engine is INNODB so fultext search concept also fails
What about
SELECT muid,actor_name FROM table_movie WHERE movie_list LIKE '%,$movieID,%'
OR movie_list LIKE '%,$movieID'
OR movie_list LIKE '$movieID,%'
OR movie_list LIKE '$movieID'
?
Edit: I modified the query to take the comments into account. A bit ugly but I guess it would work. Forget about performance. Another problem would be titles with comma in it.
If you can modify the schem you could have a 'movie' table and a 'actor_movie' table.