Skip to content

Tag: mysql

How does a multi-column index work in MySQL?

More specifically, what data structure does MYSQL use for multi-column indexing? I know MYSQL use Btree for indexing, which can only index one column. How does multi-column indexing work then? Answer Think of a MySQL “composite” index this way. Concatenate all the columns in the index together, th…

Check value of last 4 rows

I want to check the value of the last 4 rows from DB. If these 4 rows have a specific value, Do something. So the table looks like this: Here is a fiddle to test with: http://sqlfiddle.com/#!9/1064b3/1 I can run the following query SELECT * FROM testing ORDER BY id DESC LIMIT 4, Then check with PHP: Is there …

SQL Error 3780 when using foreign key as primary key

I’m getting the following order trying to use a table’s primary key as another table’s primary key: ERROR 3780 (HY000): Referencing column ‘optimization_id’ and referenced column ‘optimization_id’ in foreign key constraint ‘optimization.main – optimization…

Delete all SQL rows except one for a Group

I have a table like this: Schema (MySQL v5.7) id userid date 1 1 2021-11-15 2 2 2021-11-15 3 1 2021-11-13 4 3 2021-10-13 5 3 2021-09-13 6 2 2021-09-13 View on DB Fiddle I want to delete all records which are older than 14 days, EXCEPT if the user only has records which are older – than keep the

Slow Querying DB

I am currently optimising a system with many connected tables. The part that I am working on right now is displaying table orders. The problem is that in this table there are also many relations (around 10) which I am querying. The problem itself is in querying that many relations. I have been using Eloquent …

MySQL match a value with wildcard in multiple columns

I have a teachers table that looks like this: teacherid teacherfname teacherlname salary 1 Alexander Bennett 55.30 I would like to return any record that contains a given string in the teacherfname, teacherlname and salary columns. What I have right now (this returns exact match only): What I would like to do…