Skip to content

Tag: mysql

Select horses with logical operators [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers. This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers. Closed 9 m…

SQL Update Table 1 when Table 2 contains a specific value

I want to update lasstock of table s_articles to 1 when laststock is 0 and is either in categoryID 242, 243 or 244 in table s_articles_categories_ro. I came up with this SQL, which doesn’t work. I have these 2 tables: s_articles id laststock 1 0 2 1 3 0 4 0 s_articles_categories_ro id articleID category…

MySQL Foreign Key ERROR. Constraint key error

I can’t add a foreign key to an existing table. The error is as follows Answer I’ve written the schema below which is working. I advise against using the same name student_id for the constraint as already used for the column. I would prefer fk_student_id to avoid risk of an ambiguity or conflict a…

Query table with composite primary key, excluding specific rows

I would like to know which unique ids belong to category A but NOT category B. In the example above, only id: 2 would be a valid result I came up with this which does not work as intended: A valid approach would be to use a subquery Is it possible to do this with a single query? Answer Use

Making a MySQL COUNT query

after 4 days of tries and researchs over the web I can’t make my wishes working. Any help would be appreciated ! Performed on MYSQL WORKBENCH I got a DB made of 3 tables : user Posts likes username id id id title userId (foreign key from user.id) email content postId (foreign key from posts.id) password…

Unable to filter SQL data from last 30 days

I want to collect data from SQL from the last 30 days. This code shows data of all time this shows nothing, instead of showing last 30 days data. All SQL entries were made within the last 30 days. also tried What am I doing wrong here ? Answer Try this: The second doesn’t work because you put GROUP BY

Convert SQL commands to Python

I have the following code in SQL: I’ve been trying to rewrite it in python like so: but I keep getting a generic error message. What am I doing wrong? EDIT: added the error message Answer IIUC, you could try the following: The equivalent of SELECT DISTINCT col is drop_duplicates(col) and the equivalent …

How to avoid showing duplicate rows because conditions

The problem is where name_1 writes something to name_2 and name_2 writes something to name_1 there is a duplicate and as one result because I SELECT name_1, name_2 with condition How is it possible to show results with no duplicate rows, I only want to know if there is communication between user_1 and user_2 …

Suggested way to resolve column name/type in a view

I have the following problem that I’m trying to find the best solution for. Let’s say I have a view such as the following: What would be the fastest way to resolve the field names and types of the view? For example, on the above I am looking to get something along the lines of: The first approach …