mysql combining records from one table - mysql

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.

Related

How to Count Duplicated Names in a Varchar/Text Column in MySQL/Any SQL

So, this is the situation: I have a CSV file who looks like this:
show_id title cast
1 Batman Robert Pattinson, Collin Farrel, Zoë Kravitz
2 Twilight Robert Pattinson, Kristen Stewart
3 Ava Jessica Chastain, Collin Farrel
What I need to do is open this CSV in a Python function, do some stuff to fix white spaces and that.
Then I need to upload that in a SQL database, (whatever I want, but I choose MySQL), no problem with that.
PROBLEM
My main issue is that then I need (because mi challenge said that) to create a Query to count how many times an actor appears in all the movies in the list. So, in this case the query should display something like this:
Actor Amount_of_movies
Robert Pattinson 2
Collin Farrel 2
Zoë Kravitz 1
Kristen Stewart 1
Jessica Chastain 1
As you can see I don't have a name to search with LIKE or CONTAINS. So, how can I do that? Because, in the CSV, the list of actors per movie has, well, more than 1 actor, and I save them in a varchar or text type in the database, so each row has more than one actor.
Or should I create another table with actors and foreing keys to movies? Or is something than I can't do in MySQL but in other SQL is possible?
If you look for something performance efficient you should rather split the data (create 3 tables in total - movies, actors, casts) and connect actors with movies using the casts, then write simple sql with some joins like:
Select actors.name as Actor, count(movies.title) as Amount_of_movies from actors,
inner join cast on cast.actor_id = actors.actor_id
inner join movies on movies.movie_id = cast.movie_id;
You are able to do it another way using https://sebhastian.com/mysql-split-string/ or using psql / plsql and stored procedure. I would just split the data if it's possible.
Cheers.

mysql query to search multiple values in comma separated list

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

mysql optimize data content: multi column or simple column hash data

I actually have a table with 30 columns. In one day this table can get around 3000 new records!
The columns datas look like :
IMG Name Phone etc..
http://www.site.com/images/image.jpg John Smith 123456789 etc..
http://www.site.com/images/image.jpg Smith John 987654321 etc..
I'm looking a way to optimize the size of the table but also the response time of the sql queries. I was thinking of doing something like :
Column1
http://www.site.com/images/image.jpg|John Smith|123456789|etc..
And then via php i would store each value into an array..
Would it be faster ?
Edit
So to take an example of the structure, let's say i have two tables :
package
package_content
Here is the structure of the table package :
id | user_id | package_name | date
Here is the structure of the table package_content :
id | package_id | content_name | content_description | content_price | content_color | etc.. > 30columns
The thing is for each package i can get up to 16rows of content. For example :
id | user_id | package_name | date
260 11 Package 260 2013-7-30 10:05:00
id | package_id | content_name | content_description | content_price | content_color | etc.. > 30columns
1 260 Content 1 Content 1 desc 58 white etc..
2 260 Content 2 Content 2 desc 75 black etc..
3 260 Content 3 Content 3 desc 32 blue etc..
etc...
Then with php i make like that
select * from package
while not EOF {
show package name, date etc..
select * from package_content where package_content.package_id = package.id and package.id = package_id
while not EOF{
show package_content name, desc, price, color etc...
}
}
Would it be faster? Definitely not. If you needed to search by Name or Phone or etc... you'd have to pull those values out of Column1 every time. You'd never be able to optimize those queries, ever.
If you want to make the table smaller it's best to look at splitting some columns off into another table. If you'd like to pursue that option, post the entire structure. But note that the number of columns doesn't affect speed that much. I mean it can, but it's way down on the list of things that will slow you down.
Finally, 3,000 rows per day is about 1 million rows per year. If the database is tolerably well designed, MySQL can handle this easily.
Addendum: partial table structures plus sample query and pseudocode added to question.
The pseudocode shows the package table being queried all at once, then matching package_content rows being queried one at a time. This is a very slow way to go about things; better to use a JOIN:
SELECT
package.id,
user_id,
package_name,
date,
package_content.*
FROM package
INNER JOIN package_content on package.id = package_content.id
WHERE whatever
ORDER BY whatever
That will speed things up right away.
If you're displaying on a web page, be sure to limit results with a WHERE clause - nobody will want to see 1,000 or 3,000 or 1,000,000 packages on a single web page :)
Finally, as I mentioned before, the number of columns isn't a huge worry for query optimization, but...
Having a really wide result row means more data has to go across the wire from MySQL to PHP, and
It isn't likely you'll be able to display 30+ columns of information on a web page without it looking terrible, especially if you're reading lots of rows.
With that in mind, you'll be better of picking specific package_content columns in your query instead of picking them all with a SELECT *.
Don't combine any columns, this is no use and might even be slower in the end.
You should use indexes on a column where you query at. I do have a website with about 30 columns where atm are around 600.000 results. If you use EXPLAIN before a query, you should see if it uses any indexes. If you got a JOIN with 2 values and a WHERE at the same table. You should make a combined index with the 3 columns, in order from JOIN -> WHERE. If you join on the same table, you should see this as a seperate index.
For example:
SELECT p.name, p.id, c.name, c2.name
FROM product p
JOIN category c ON p.cat_id=c.id
JOIN category c2 ON c.parent_id=c2.id AND name='Niels'
WHERE p.filterX='blaat'
You should have an combined index at category
parent_id,name
AND
id (probably the AI)
A index on product
cat_id
filterX
With this easy solution you can optimize queries from NOT DOABLE to 0.10 seconds, or even faster.
If you use MySQL 5.6 you should step over to INNODB because MySQL is better with optimizing JOINS and sub queries. Also MySQL will try to run them into MEMORY which will make it a lot faster aswel. Please keep in mind that backupping INNODB tables might need some extra attention.
You might also think about making MEMORY tables for super fast querieing (you do still need indexes).
You can also optimize by making integers size 4 (4 bytes, not 11 characters). And not always using VARCHAR 255.

Selecting data from two tables

Hello everybody
Well my question is about sql commands...
If I have 2 tables with the same number of columns and the same fieldnames (e.g: A(n,name,date) and B(n,name,date))
In the website, I want to retrieve data from both tables and display them in order by date descendent.
(The use of two tables is due to difference in tables database or server,or just the use of every table.. sometimes there's a need to display both tables in one order)
exemple
table Sport_news(N_event,Title,Texte,Date)
table International_news(N_event,Title,Texte,Date)
Display:
Christiano Ronaldo ... 2011/25/01
christiano ronaldo is one of the famous...
Barack Obama president of the USA... 2011/24/01
Barak obama........
The arsenal has... 2011/23/01
Chamakh, player of arsenal is anger.....
I hope that the idea is clear : and thank you!
You want UNION
select a.name,a.date
from table1 a
where ...
UNION ALL
select b.name,b.date
from table2 b
where ...
order by 2 desc
When you use a UNION, you specify the order by with column numbers instead of names.

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.