I have a problem with query. My database tabels: FIRST TABLE SECOND TABLE For example like a Post and comments. I want newest comment in post. Expected Output: My query OUTPUT: I tried with MAX(date_add) but don’t work or I do it something wrong Answer You can use a subquery with MAX() aggregation for d…
Tag: mysql
MYSQL Update Column with higher values from other table
I’m having two tables in an mysql database. TABLE_A I want to update Table_A with values from Table_B, but only if they are higher. Has anyone a snippet for me? Answer One option uses the update/join syntax: If there are no duplicate ids in table_b, no need for aggregation:
how to successfully add a foreign key constraint to your Database table in laravel using eloquent?
I have a database containing two Tables(students, payments) with id as primary key and another column studentID within the students Table. Am making studentID a foreign key in payments Table. I am getting this error message: SQLSTATE[42000]: Syntax error or access violation: 1072 Key column ‘studentID&#…
Need a Query to fetch distinct data from a table in my sql
I have a table History which contains below columns: I need a query to fetch data from a table in a manner that every user should return only one row for the day even if a user has more than one entry for a day The required resultset should be as below : Answer You can group by User and
Counting Number of Males/females within a blood group
So I have two Data Tables ‘DONOR’ AND ‘ACCEPTOR’. Each have the following schema: DID|NAME|GENDER|CITY|BG |AMOUNT INT|STR |CHAR |STR |STR|INT I’m trying to create a query to output the Gender and …
Find rows using in between values in MySQL
I found it really hard to come up with a descriptive problem title, so I’ll give a bit of context. We have a list of boats and a list of docks. Boats can be transferred from one dock to the other on a specific datetime, or it could be at no dock at all. I’m trying to find out which
Mysql – Finding a record that satisfies 2 exact dates
Fairly new to mysql & the stack overflow community. – I have a question that’s been bugging me. I’m trying to return the results of individuals that have taken out a book on 2 specific dates. This is my effort so far; I’m not sure why I’m not getting any results – When …
foreign key column doesn’t want to take an insert says column can’t be null
i’m trying to populate a foreign key column ‘boss_id’ from a junction table called ‘clown_boss’ boss_id is new self-referencing foreign key that is meant to hold the ‘id’ value of the clown’s boss. i already have a populated junction table that contains ‘i…
how to INSERT new record with SELECT value in sql
How can i insert a value into table A using return value from table B. I know i can use INSERT INTO SELECT statement but this requires that data types in source and target tables match and the columns are in order but i have the below table like thus: Table A: Table B: Now i want to count all
Can I use Where Clause like this for my query.. help me [closed]
Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 2 years ago. Improve this question Answe…