duanpendan8067 2013-11-29 03:57
浏览 48

如何从一个表中获取数据并在同一查询中使用这些值?

I have two tables. And i want to get data from second table corresponding to value in one row from the first table.

I got two tables promos & stores

So what I'm trying to do is first pull ONE random row from PROMOS first and based on that row also get corresponding data from STORES.

the "category" column in PROMOS table = STORES (the name of second table)

promos.category = stores


promos has the following columns : id,image,category,storeid

stores is the second table that has columns: sid, storename, storeimage

Row in PROMOS table : 1, promopic.png, stores, 2

Row in STORES table : 2, shoppers, shoppers.png,

--


UPDATE: what I'm trying to do is the "stores" name is a row in promos. so the name of the second table i select data from is unknown until we get it from promos table first..

I've tried something like this

SELECT * FROM promos, promos.category WHERE promos.category.sid ='promos.storeid'

  • 写回答

2条回答 默认 最新

  • dongzouqie4220 2013-11-29 04:02
    关注

    Try the following ;)

    set @table_name = (SELECT category FROM promos where id=1); --change the 1 to whatever
    set @sql =  CONCAT("select * from ",@table_name);
    PREPARE query FROM @sql;
    EXECUTE query;
    

    The above assumes that you want to select everything from the table name we got from promos.category

    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏