首页 \ 问答 \ 一个父行,另一个表中的多个子行。(One parent row, multiple child rows in another table. How to get them all in one row?)

一个父行,另一个表中的多个子行。(One parent row, multiple child rows in another table. How to get them all in one row?)

我是一名当地非营利组织的志愿者,在节日期间赠送玩具和食物。 我帮助他们的一种方式是在网上提出他们的礼物/食物要求。 这样,您可以在15个小时内手动将1000个左右的名称手动输入到Excel中,只需单击链接即可在几秒钟内导出整个内容。

我的问题与如何在一个查询中从数据库中获取数据有关。 我相信这是可能的,但不能完全包围我能做到的。

有一个父表,其中包含将收到礼物的孩子的家庭信息(每​​个家庭1行)。 有一个子表,其中包含家庭的孩子的信息(每个孩子1行,每个父行有很多行)。 每个父行将有1-3个子行。

PARENT TABLE
-----------------------------
id
name
address
city

CHILDREN TABLE
-----------------------------
parent_id
name
gift1
gift2
gift3

在从数据库导出数据时,我希望将所有孩子的信息与父母的信息一起获取,以便将其放入CSV文件中。 我知道我可以通过两个单独的查询来执行此操作,但如果可能的话,我不想为每个家庭的孩子访问数据库。

我已经思考了一个小时左右,并且无法想出我将如何实现这一目标。 我确定我需要加入这些表,但我不确定如何区分每个孩子并避免重复它们而不是让下一个孩子(如果有的话)。


I volunteer for a local non-profit that gives out toys and food during the holiday season. One way that I am helping them is by putting their gift/food requests online. This way instead of manually entering 1,000 or so names manually into Excel over the course of 15 hours they can simply click on a link and have the whole thing exported in the matter of seconds.

My issue is related to how I can get the data from the database in one query. I believe it is possible but can't quite wrap my head around I can do it.

There is a parent table which contains the family information for the kids who will receive gifts (1 row per family). There is a child table that contains the family's childrens' information (1 row per child, many rows for each parent row). There will be 1-3 children rows for each parent row.

PARENT TABLE
-----------------------------
id
name
address
city

CHILDREN TABLE
-----------------------------
parent_id
name
gift1
gift2
gift3

I would like to get all of the childrens' information with the parents' information in one row when exporting the data from the database so it can be put into a CSV file. I know I can do this with two separate queries but I don't want to hit the database for each and every family's children if possible.

I've pondered this for an hour or so and can't quite come up with how I would accomplish this. I'm sure I need to join the tables but I am unsure how to differentiate each child and avoid duplicating them instead of getting the next child (if there is one).


原文:https://stackoverflow.com/questions/3512948
更新时间:2023-07-31 08:07

最满意答案

你快到了。 您只需要通过lambda或functools.partial传递名称:

newbuttonname = username 
newbuttonname = Tkinter.Button(win, text = newbuttonname, 
    command = lambda name=username:click_one(name))

您可以使用相同的技术传递实际的wodget或任何其他数据。


You are almost there. You simply need to pass the name via lambda or functools.partial:

newbuttonname = username 
newbuttonname = Tkinter.Button(win, text = newbuttonname, 
    command = lambda name=username:click_one(name))

You can use this same technique to pass in the actual wodget, or any other data.

相关问答

更多
  • def b2cmd(event): print(event.widget['text']) b2=Button(root,text = 'b2') b2.pack() b2.bind(" ",b2cmd)
  • 我想你想要这样的东西: from tkinter import * from tkinter import ttk root = Tk() board_frame = ttk.Frame(root, padding=5) board_frame.grid(column=0,row=0) COORDS_LIST = [] buttons_dict = {} ########################################### def fire_here(x, y): print ...
  • 假设您以通常的方式创建按钮,您可以使用lambda传递参数。 lambda允许您使用参数创建一个匿名函数,然后您可以使用它来调用您的函数。 如果要传递实际的按钮引用,则需要分两步执行,因为按钮对象在创建之后才会存在。 for i in range(10): button = tk.Button(...) button.configure(command=lambda b=button: move(b)) 你的move功能需要如下所示: def move(var): mGridv = ...
  • 感谢Bryan Oakley,最后我注意到用Mac改变按钮高度是不可能的,他建议使用标签作为替代,这可能是解决问题的最佳选择。 再次感谢您的帮助。 Thanks to Bryan Oakley and finally I noticed that it is impossible to change the button height with Mac, and he suggested using label as alternative which is probably the best choice ...
  • 你快到了。 您只需要通过lambda或functools.partial传递名称: newbuttonname = username newbuttonname = Tkinter.Button(win, text = newbuttonname, command = lambda name=username:click_one(name)) 您可以使用相同的技术传递实际的wodget或任何其他数据。 You are almost there. You simply need to pass ...
  • 该按钮不可见,因为它的父( my_gui )不可见。 您需要在my_gui上使用pack , place或grid使其可见。 例如: my_gui = Resizer(root) my_gui.pack(fill="both", expand=True) The button isn't visible because it's parent (my_gui) isn't visible. You need to use pack, place or grid on my_gui to make it v ...
  • 使用wraplength选项是一个很好的方法来实现它的换行,并使用justify选项来执行除center之外的其他操作。 option1 = tkinter.Button(opt1, wraplength=80, justify=LEFT, text='This is the text') wraplength的值以屏幕单位或像素为单位。 您也可以在字符串中间放一个'\n'字符来手动分割线条,但如果您这样做,也可能必须手动调整按钮大小。 Using the wraplength option is a g ...
  • 如果你想让按钮有一个处理函数,你需要在使用这个函数之前声明它,如下所示: def activate(): os.system("networksetup -setairportpower airport on") ActivateButton = Button(text="Activate", fg="green", bg="black", command=activate) 所以你需要切换你的部分#Activate/Deactivate command在脚本中#Activate/Deactiv ...
  • 要字面回答你的问题: 是否有一个函数来获取Tkinter中小部件的主框架的名称? winfo_parent正是您所需要的。 为了有用,您可以将它与_nametowidget结合使用(因为winfo_parent实际上返回了父代的名称)。 parent_name = widget.winfo_parent() parent = widget._nametowidget(parent_name) To literally answer your question: Is there a function to ...
  • winfo_height返回以像素为单位的值。 除非按钮或标签仅包含图像,否则按钮和标签的height选项会将值视为平均大小的字符数。 您可以使用带有适当选项的grid来确保每个窗口小部件的大小完全相同,而不是使用place 。 布局GUI时, place很少是正确的选择。 Tkinter非常擅长计算尺寸和位置。 winfo_height returns values in pixels. The height option for buttons and labels treats the values ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)