How can i select a table dynamically from mysql - mysql

Is there any option to select a table dynamically from mysql..
For example,
If i have 3 tables like
t_tableconfig
t_2013
t_2014
t_tableconfig contains data like,
tableid tablename
1 t_2013
2 t_2014
t_2013 contains
id name
1 David
t_2014 contains
id name
1 joe
If I will pass a table name as a parameter.. Can I view the records from specified table? Please advice me

It depends on what your programming language or ORM(Object-relational Mapping).
Could you please tell me the environment you're using to query sql statement?
// I've added below
As you said you're using hibernate, I hope the link below can help you.
I'm not familiar with hibernate.
Hibernate: Data Object with a dynamic table name by Annotations

Use redbeans ORM . it's awesome ORM. you can change your table dynamically. You can perform crud operation which you usually use in several tools.
Advantage
You can create schema more faster.
Magically perform [CRUD](http://en.wikipedia.org/wiki/Create,_read,_update_and_delete) operation
That has only 1 php file. It has no criteria to installation.
Easy to learn

Related

Creating a newsfeed in cshtml

I have the following in database
status table - status_id, status_body, status_date
student table - student_id, student_username, student_firstname, student_lastname
follow table - followId, follow_followerid, follow_followingid`
Now, I want to show the status of the users which a user(say user A) currently follows along with his(user A) own status updates.
How can I do it in cshtml? SQL query for this is necessary.
Any help?
You can't put a sql query in a cshtml file. There is a chain of events that have to happen. You serve the cshtml with only the results of your database query in it. You also need some server-side code to serve the cshtml, for example c# using MVC. And you need a way to access your database, for example an object-relational mapper such as Entity Framework. You have a lot of steps, if you want to make this work.

Solr index multiple tables from MySQL

I have following mysql tables
1. user(user_id,email)
2. tweets(tweet_id,user_id,tweet)
3. tags(tag_id,tag)
4. tweets_tags(tweet_id,tag_id)
I want to show current user's tweets under "My Tweets" Tab in application. I want to get following data from Solr
user_id
email
tweet where user_id=x
tags where tweet_id=xx
How to index those mysql table on Solr? I only what to know the code of schema.xml and data-config.xml for Full/Delta import.
Note : I am not asking about MySQL connector etc, I have done already.
The use case you've described doesn't seem to justify using solr. You would just make sure you have proper keys and indexes and do it in mysql directly.
If for some reason you MUST use solr, you could probably prepare all the data and feed it to solr in a tag/tweet/user structure like this
user1 - tweet1 - tag1
user1 - tweet1 - tag2
user1 - tweet2 - tag1
and so on.
Then from solr you query by user, and then sort and group by tweet and then tag.
However I must state again that the solution I just described is implemented much safer with a higher confidence on the result by using plain sql.
Should you provide more details on your desired outcome, I'd be happy to suggest the database structure along with the necessary foreign keys and indexes and the queries you need to get your data out.
If you are using DIH (dataimporterhandler), I guess that link should be the solution for you:
Import with sub entities
If you have problem with writing the exact configurations, please let me know, I can assist you.

Replace row names in an SQL statement with informations from an other table

I have a MySQL table named data where the name of a column is field_id_# (where # is a number from 1 to 129). I also have another table named fields with columns field_id (with only the # of the corresponding field in table data - That means just the number # not field_id_#) field_name and field_label. Now I would like to run a query like this:
SELECT data.field_id_1 AS fields.field_label1,
data.field_id_2 AS fields.field_label2
[...]
I don't know if this is possible or not and if so, how to do it.
Can someone help me with that?
Thanks for your help.
Please use the meaningful names directly for your column definitions! All your selects and program parts that access the database will be readable. They're surely not readable with the naming convention you describe here.
For an immediate solution you can build the SQL Statement dynamically/programmatically, or you generate views where the columnnames are replaced with the meaningful ones.

Correct foreigen key approach in jpa Or data saving basic approach

This question of mine is subjective
i am getting a list of objects from a third site.
now i want to save that data in database.
suppose the data is List. This response is to a query that i fired to that site .
now i want to save two things
1) query name
2) the response(List) (answer)
the myobject can have lot of answers corresponding to my query. now i want to save all these answers separately so that each answer can be fetched independently.
now i have this DB approach
one table for query and query id
second table which will consist of query id and query answer. (which will be foreigen key in first table
My question is am i following right approach?
initially i thought of saving the whole list in database but as per my knowledge we can not save list in database directly although in jpa implementation 2.0 we can save list in db (correct me if i am wrong)
please guide me with my current approach or of there is any better approach
i am using JPA 2.0 eclipselink.
Regards
Anil Sharma
What is your object model?
You can use OneToMany or ManyToMany to store a collection of Entity objects.
If you have a List or List you can store this using an ElementCollection.
But you may be better off creating an Answer or AnswerReference Entity.
See,
http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection

How to parse this LONGTEXT string field in MySQL

I need to extract the following fields into a new table. Any ideas whether I can do this exclusively with a query or I need to use PHP as well?
Current table structure
USERID USEREXPERINCE
1 a:4:{i:0;s:20:"business development";i:1;s:6:"design";i:2;s:9:"marketing";i:3;s:15:"press relations";}
Required table structure
USERID USEREXPERINCE
1 business development
1 design
1 marketing
1 press relations
2 web development
2 design
3 marketing
3 business development
Thanks.
You need to use PHP - the 'LONGTEXT' data is in fact a serialized PHP array.
Execute the following to see what I mean:
<?php
print_r(unserialize('a:4:{i:0;s:20:"business development";i:1;s:6:"design";i:2;s:9:"marketing";i:3;s:15:"press relations";}'));
?>
As such, the easiest thing to do would be to read each row from the database, unserialize the data and insert it into a new table with the required fields. (I'm guessing you need to search on these, hence the need to store them as dedicated fields.)
That said, the serialized string you provided only appears to be storing IDs -> Field names (rather than any values), so I'm not sure what's going on there.
I would use PHP for this, simply because it is easier to call unserialize() and generate new INSERT statements than to parse the string in a MySQL procedure (though that could also be done). Also beware if your USERID column is currently a primary key, since it cannot be with the new structure.