Skip to content

Tag: mysql

How to move ID of duplicate names to a separate column

I have a MySQL table with two duplicate names, how can I separate the IDs of the duplicate names into a separate column? For example, like this: This is my current SQL statement to retrieve the current results in the first table: There is only one duplicate at most for each name. Thanks Answer If you are sure…

Subtract row values of 2 columns within same table in SQL query

I’m trying to subtract the row values of 2 different columns in the same table. Table schema I want to subtract com – refund if support column is not NULL and status = 1 Output should be like as Answer Try this: The ‘as output’ names the column in your result set. The where statement f…

use quotes in sql query node.js

I have my query like so: but when I insert the query into my database (I am storing the query for analytical purposes), it fails because of the quotes. how can I fix this? I tried converting thequery to toString(), but that was a useless attempt. Any other ideas? edit: error i am recieving is: Answer This is …

Convention for IDs of child tables

Every tutorial that I watched implemented child tables with two IDs, one ID for the table itself, and one ID that was just a reference to the parent’s table ID, like so: In my project, I’m doing a discord bot, and the only identifier that I need is the server’s ID. There’s no need for …

Filter table to leave out specific rows

I have the table and now I want to list every ProjectName where there is no specified Leader like this: Right now I only know how to filter all ProjectNames with Leaders, but not the way to filter them the other way! Any help is appreciated 🙂 Answer One way to do it is with aggregation and the condition in

How to get the first row per group?

I have a query like this: The result looks like this: Now I want to get the first row for each category_id. So it must be the biggest num and its business_id. So the expected result would be: How can I do that? Answer if your MySQL version support ROW_NUMBER + window function, you can try to use ROW_NUMBER to

SQL Query: Search Across Multiple Fields in JOINed Tables

SQL Version: MySQL 8.0 or SQL Server SQL Fiddle: https://www.db-fiddle.com/f/wcHeXkcynUiYP3qzryoYJ7/6 I have a table of images and a table of tags that link to those images. I want to be able to pass (2) tags to the query and have it select only the images that match BOTH tags. For example, I want to pass Nov…