Sie sind auf Seite 1von 1

Search… Log In Sign Up

Home

PUBLIC

Stack Overflow

Tags
How to select from MySQL where Table name is Variable Ask Question
Users

Jobs I have a case where getting the table name should be from a set variable like: asked 6 years, 10 months ago

viewed 41,559 times


SET @ID_1 = (SELECT ID FROM `slider` LIMIT 0,1);
42 SET @Cat = (SELECT Category FROM `slider` LIMIT 0,1);
Teams active 8 months ago
select * from @Cat where ID = @ID_1
Q&A for work

but doing that way MySQL outputs an error, so could someone show me how I can achieve that,
Learn More
because these are my baby steps in MySQL. Looking for a job?
10
mysql set tablename Software Engineer (Full-stack)
Generation Ciudad de México, Mexico
mysql reactjs
share improve this question edited Feb 28 at 18:19 asked Jan 10 '12 at 20:19
Jacob Alvarez Burimi
2,131 1 6 15 7,430 9 48 86 Full Stack Engineer- Ruby on Rails
Elastic No office location
add a comment
REMOTE

elasticsearch reactjs
1 Answer active oldest votes

Product Development Engineer


Tom Sawyer Software
¿No encuentras la respuesta? Pregunta en Stack Overflow en español. ✕ Ciudad de México, Mexico
c# java

You'd have to do this with a prepared statement. Something like: Full-Stack Developer, WordPress
Sanborn No office location
SET @s = CONCAT('select * from ', @Cat, ' where ID = ', @ID_1); $60K - $90K REMOTE
68
php nodejs
PREPARE stmt1 FROM @s;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
Linked
share improve this answer answered Jan 10 '12 at 20:25
0 how to use a value from sql result as table
Joe Stefanelli name for its sub query?
109k 13 189 203
How to select a table in query as variable in
0
Join Stack Overflow to learn, shareBy
knowledge, andyou
using our site, build
acknowledge that you have read and understand our Email
your career. CookieSign UpPrivacy Policy,
Policy, OR SIGNand
IN WITH Google
mysql?
our Terms of Service. Facebook
One more thing it show resul only when i remove DEALLOCATE PREPARE stm1; – Burimi Jan 10 '12 at
20:37 0 MySQL: Count rows of a table that is
named in the current table
Brilliant solution. Helped me to solve the issue I'm having, thanks. – Tim Visée May 26 '16 at 12:36
1 In MySQL trigger, is it possible to set a
What does the DEALLOCATE PREPARE do? – Frozen Flame Jun 29 '16 at 2:59
user variable with NEW.col and use that in
update query?
1 @FrozenFlame, release the stmt1 . If you do not release it, may you encounter the max limit of statements,
enforced by max_prepared_stmt_count variable system, how you can read here. – ruzenhack Jul 12 '17 at -2 Declaring variables in SQL and “Must
19:29 declare the table variable @TBL” error
is this still valid syntax in 2018?? – Anthony Jul 17 at 1:51 0 Select from table with IF

show 1 more comment


0 Error in writing dynamic query in MySQL

0 Database Structure, Storing and Organizing


Your Answer Data

0 UPDATE multiple table with same column


name with condition for each value on
MYSQL

0 MySQL Local Variable

see more linked questions…

Related

830 How do I quickly rename a MySQL


database (change schema name)?

702 How to find all the tables in MySQL with


specific column names in them?

995 How do I connect to a MySQL Database in


Python?

1245 How to get a list of MySQL user accounts

Sign up or log in Post as a guest 124 How do I add more members to my ENUM-
type column in MySQL?

713 Insert into a MySQL table or update if


Sign up using Google Name
exists

1024 How to reset AUTO_INCREMENT in


Sign up using Facebook Email MySQL?

required, but never shown 674 How to get the sizes of the tables of a
MySQL database?
Sign up using Email and Password
949 Reference - What does this error mean in
PHP?
Post Your Answer 1404 How to import an SQL file using the
command line in MySQL?
By click ing "Post Your Answer", you ack nowledge that you have read our updated terms of service, privacy policy and cook ie
policy, and that your continued use of the website is subject to these policies. Hot Network Questions
Possible definitions of exponential function

Not the answer you're looking for? Browse other questions tagged mysql set tablename or ask your Why do the British use the word "flipping" for
emphasis?
own question.
Alternating items in a collection

Can you drop one hand from a two-handed


weapon to draw and use a single-handed
weapon next turn?

Using an Rj45 to D9 RS232 Adaptor with a laptop


to control an RS232 device

more hot questions

question feed

STACK OVERFLOW PRODUCTS COMPANY STACK EXCHANGE Blog Facebook Tw itter LinkedIn
NETWORK
Questions Teams About
Technology
Jobs Talent Press
Life / Arts
Developer Jobs Directory Engagement Work Here
Culture / Recreation
Salary Calculator Enterprise Legal
Science
Help Privacy Policy
Other
Mobile Contact Us site design / logo © 2018 Stack Exchange Inc; user contributions
licensed under cc by-sa 3.0 w ith attribution required.
Disable Responsiveness rev 2018.11.7.32103

Das könnte Ihnen auch gefallen