Posts

Showing posts with the label mysql

Mysql last hour data

I tried several ways I found here, but it spits out more than the last hour. The table has a field t_timestamp with data like: It is now 2018-06-29 01:55:04 Above was created although I used something like: Where is my thinking error? I believe the solution is to this is to use TIMESTAMPDIFF instead of the DATE_SUB . Try something like the following: WHERE TIMESTAMPDIFF(SECONDS, now(), t_timestamp) < 3600000 Hope this helps. Usage of MySQL TIMESTAMPDIFF Demo :http://sqlfiddle.com/#!9/f5e2c3/8/0 Solution: By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

How to insert data if not exist else update it?

I want to update row if exists in table bag else insert new row these types of columns that I used it in table bag I make car_name and p_id as primary (composite key) I write this code in MySQL routine in xampp and it saves in xampp without errors but when I tries to execute it in xampp it makes error that says The following query has failed: "SET @p0='mohamed'; SET @p1='1'; SET @p2='1'; SET @p3='1'; CALL bag_insert (@p0, @p1, @p2, @p3); " MySQL said: Code: By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Mysql Queries in big data table

I have problem with my mysql database table. I have more then 20 millions rows in table. Table structure shown below. Main problem is that queries take really long time to execute (some queries take more then 20 second). I use indexes where i can, however many queries use date range and with date range my indexes don't work. Also in queries i use almost every column. What i need to change to my data table, to improve efficiency? Some of my queries: Your first query is better written as: Why? Then, for this query, a reasonable index is history(action, place, created_at, deleted_at) . So, I would start with multi-column indexes. If you continue to have performance issues, you should then consider partitioning the data based on the created_at date. INDEX(a), INDEX(b) serves some purposes, but the "composite" INDEX(a,b) better serves some queries. Needs I prefer to write the date range thus: It's a lot easier than dealing with leap year, end of year, etc. And it works...

Laravel and Mysql three tables relationship

I need to add three tables to MySql but I don't know how to organize them. My tables are : And my relations : Surfaces are enter after apartment, so I can't use surfaces floor as my apartment floor. At the moment I have apartment table and floor table with a pivot table (many to many). In Mysql, what's the best way to handle this ? And after inside laravel ? Thank for yourhelp ! Edit : my work on laravel so far (only floor table and apartment table) In Apartment.php : In Floor.php : Edit 2 : my migration so far : I can think of below kind of relationships in Laravel, I have not created the sample code although but it should help. Apartment model: Floor model: Surface model: However , it will bring more clarity to the answers if you can provide table schema or migration schema on the same question. Also, you can mention which kinds of SQL queries you are expecting and want to execute. Thanks. Hope this helps. By clicking "Post...

MariaDB Multiple Row Insert Order

Please consider below query - I want to insert 2 record with same 'categoryId'. To genereate categoryId I am using mariadb sequence https://mariadb.com/kb/en/library/sequence-overview/. NextVal() return next sequence number and LastVal() return last sequence number (same as NextVal() function). Above query is working fine if INSERT statement order is left to right(I found in my test). Example: " insert into x values('1'), ('2') ". '1' will insert first then '2', But I am not sure mariadb will always follow this order or not. Please help me about INSERT order or suggest me some different query. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.