Save the query statements and results in mysql - mysql

my homework require me to execute those queries and show the result. Thing is I can export the table into CSV or excel just fine, but the TA require me to somehow show the exact command line that give it the table.
Is there some statements like log or something to do that?

You can use the mysql command from the shell with the -v option, it will display the query followed by the results:
$ mysql -v -e 'select * from barmar_test';
--------------
select * from barmar_test
--------------
+-----------+-----------+---------+------+------+---------------------+------+
| username | timestamp | partner | e | en | t | ttx |
+-----------+-----------+---------+------+------+---------------------+------+
| foo | NULL | abc | N1 | NULL | 2013-07-03 23:27:47 | NULL |
| foo | NULL | abc | 1 | NULL | 2013-07-03 23:25:12 | NULL |
| NULL | NULL | abc | 12 | NULL | 2013-07-03 23:25:12 | NULL |
| foo | NULL | abc | NULL | NULL | 2013-08-30 15:01:39 | NULL |
| foobarbaz | NULL | abc | NULL | NULL | 2013-09-02 13:04:00 | NULL |
+-----------+-----------+---------+------+------+---------------------+------+

Related

What does a sql query look like when using the belongsToMany method in laravel 6.0?

What does a sql query look like when using the belongsToMany method in laravel 6.0?
For example, we have three tables in the database:users, roles, role_user.
users
+----+-------+-----------------+-------------------+----------+----------------+------------+------------+
| id | name | email | email_verified_at | password | remember_token | created_at | updated_at |
+----+-------+-----------------+-------------------+----------+----------------+------------+------------+
| 1 | admin | admin#gmail.com | NULL | admin | NULL | NULL | NULL |
| 2 | user1 | user1#gmail.com | NULL | user1 | NULL | NULL | NULL |
| 3 | user2 | user2#gmail.com | NULL | user2 | NULL | NULL | NULL |
+----+-------+-----------------+-------------------+----------+----------------+------------+------------+
roles
+----+---------+-------------+------------+------------+
| id | name | description | created_at | updated_at |
+----+---------+-------------+------------+------------+
| 1 | admin | NULL | NULL | NULL |
| 2 | user | NULL | NULL | NULL |
| 3 | editor | NULL | NULL | NULL |
| 4 | manager | NULL | NULL | NULL |
+----+---------+-------------+------------+------------+
role_user
+---------+---------+------------+------------+
| role_id | user_id | created_at | updated_at |
+---------+---------+------------+------------+
| 1 | 1 | NULL | NULL |
| 2 | 2 | NULL | NULL |
| 3 | 3 | NULL | NULL |
| 4 | 3 | NULL | NULL |
+---------+---------+------------+------------+
Im model User
public function roles()
{
return $this->belongsToMany('App\Role');
}
What does a sql query look like when using the $this->belongsToMany('App\Role'); method in laravel 6.0?
Laravel can easily tell you that.
$user->roles()->toSql();
For a more comprehensive experience, you can log all queries instead. See this stackoverflow post for more.
DB::connection()->enableQueryLog();
$queries = DB::getQueryLog();

Mysql - Finding missing dates with multiple join tables

I need some help writing a mysql query. I need to find all missed reports for a given date range for a specific business id / project id.
Basically, for a given business id, I need to know the name of the project and all dates that a report is either missing or not marked as completed.
I am using the calendar table trick (as described here and here) to find the missing report dates, but I am having problems joining the projects table to find the associated project / business that a report was missed for.
I basically need a result set that will give me data similar to this:
+------------+-----------+--------------+
| project_id | name | missing_date |
+------------+-----------+--------------+
| 1 | Project 1 | 2014-01-01 |
| 1 | Project 1 | 2014-01-03 |
| 1 | Project 1 | 2014-01-04 |
| 1 | Project 1 | 2014-01-07 |
| 1 | Project 1 | 2014-01-09 |
| 2 | Project 2 | 2014-01-02 |
| 2 | Project 2 | 2014-01-03 |
| 2 | Project 2 | 2014-01-04 |
+------------+-----------+--------------+
Here is my schema:
projects table:
+----------------+------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+------------------+------+-----+-------------------+----------------+
| project_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| business_id | int(10) unsigned | NO | MUL | NULL | |
| name | tinytext | YES | | NULL | |
+----------------+------------------+------+-----+-------------------+----------------+
reports table:
+---------------------+------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+------------------+------+-----+-------------------+----------------+
| report_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| project_id | int(10) unsigned | NO | MUL | NULL | |
| report_date | date | NO | MUL | NULL | |
| completed | bit(1) | NO | | b'0' | |
+---------------------+------------------+------+-----+-------------------+----------------+
calendar table:
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| dt | date | NO | PRI | NULL | |
| month_name | varchar(9) | YES | | NULL | |
| day_name | varchar(9) | YES | | NULL | |
| y | smallint(6) | YES | | NULL | |
| q | tinyint(4) | YES | | NULL | |
| m | tinyint(4) | YES | | NULL | |
| d | tinyint(4) | YES | | NULL | |
| dw | tinyint(4) | YES | | NULL | |
| w | tinyint(4) | YES | | NULL | |
| is_weekday | bit(1) | YES | | NULL | |
| is_holiday | bit(1) | YES | | NULL | |
| holiday_desc | varchar(32) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
The following query below works to return a list of incompleted reports, but I still need to fill the gaps in with the dates where there is no report record at all.
select
p.project_id,
p.name,
c.dt as missing_date,
r.completed
from reports r
join projects p on (r.project_id = p.project_id)
right join calendar c on (c.dt = r.report_date)
where c.dt >= '2014-02-01'
and c.dt <= '2014-02-10'
-- and r.report_date is null /** THE RESULT SET IS EMPTY IF I UNCOMMENT THIS **/
and r.completed = false
and c.is_holiday = false
and c.is_weekday = true
and p.business_id = 1001
order by p.project_id, r.report_date, c.dt;
Any help would be much appreciated!
I read the links you have provided and this method of storage for dates is great.
any way in your query you used:
right join calendar c on (c.dt = r.report_date)
but I don't see any report_date in your report table.
I suggest you check it if the problem is because of it ,since apart from that your query seems to work properly.
Ok, I finally got it to work. It's a fairly complicated query that requires an inner join on the projects table but I don't know a better way to do it - I'm a Java guy who relies on hibernate way too much these days to build my queries :). If anyone has a more efficient solution I am all ears!
The final query:
select
p.project_id,
p.name,
c.dt as missing_date,
r.report_date,
r.completed
from calendar c
inner join (
select
p1.project_id,
p1.name
from projects p1
where p1.project_id = 1005
-- where p1.business_id = 1001 /** OR USE THE BUSINESS ID **/
) p on c.dt between '2014-02-01' and '2014-02-28'
left join reports r on r.report_date = c.dt
and r.project_id = p.project_id
and r.completed = false
where (r.report_date is null or r.completed = false)
and c.is_holiday = false
and c.is_weekday = true
order by p.project_id, c.dt;
Produces the correct results:
+------------+--------------+--------------+-------------+-----------+
| project_id | name | missing_date | report_date | completed |
+------------+--------------+--------------+-------------+-----------+
| 1005 | Project 1005 | 2014-02-03 | 2014-02-03 | 0 |
| 1005 | Project 1005 | 2014-02-04 | 2014-02-04 | 0 |
| 1005 | Project 1005 | 2014-02-05 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-06 | 2014-02-06 | 0 |
| 1005 | Project 1005 | 2014-02-07 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-10 | 2014-02-10 | 0 |
| 1005 | Project 1005 | 2014-02-11 | 2014-02-11 | 0 |
| 1005 | Project 1005 | 2014-02-12 | 2014-02-12 | 0 |
| 1005 | Project 1005 | 2014-02-13 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-14 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-18 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-19 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-20 | 2014-02-20 | 0 |
| 1005 | Project 1005 | 2014-02-21 | 2014-02-21 | 0 |
| 1005 | Project 1005 | 2014-02-24 | 2014-02-24 | 0 |
| 1005 | Project 1005 | 2014-02-25 | 2014-02-25 | 0 |
| 1005 | Project 1005 | 2014-02-26 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-27 | NULL | NULL |
| 1005 | Project 1005 | 2014-02-28 | NULL | NULL |
+------------+--------------+--------------+-------------+-----------+
Thanks for the help guys and gals!

MySQL count 2 columns in a row and join with another query

I have a table visits like this:
+--------------+-------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| vis_id | int(11) | NO | | NULL | |
| unit | int(11) | NO | | NULL | |
| time_in | timestamp | NO | | CURRENT_TIMESTAMP | |
| time_out | timestamp | NO | | 0000-00-00 00:00:00 | |
| in_username | varchar(16) | NO | | NULL | |
| out_username | varchar(16) | NO | | NULL | |
+--------------+-------------+------+-----+---------------------+----------------+
and a table users like this:
+------------+-------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| fname | varchar(32) | NO | | NULL | |
| lname | varchar(32) | NO | | NULL | |
| date_added | timestamp | NO | | CURRENT_TIMESTAMP | |
| username | varchar(16) | NO | | NULL | |
| password | varchar(40) | NO | | NULL | |
| auth_level | int(1) | NO | | 1 | |
| last_login | timestamp | NO | | 0000-00-00 00:00:00 | |
+------------+-------------+------+-----+---------------------+----------------+
I want to be able to count how many times each user is in in_username AND out_username... The query I was using before looks like this:
select count(*) as "count", u.fname as "fname"
from visits v
inner join users as u on u.username = v.in_username
group by u.username order by u.fname
But that only returns how many in_username's there are... I'd like to have both in the same query if possible, so I could get results like this:
+----------+-----------+----------+
| count_in | count_out | fname |
+----------+-----------+----------+
| 118 | 224 | Bo |
| 27 | 64 | James |
| 147 | 138 | Jeremy |
| 23 | 37 | Jim |
| 182 | 172 | Robert |
| 120 | 158 | Tom |
+----------+-----------+----------+
Where count_in is how many times their username appears in visits.in_username, and count_out is how many times their username appears in visits.out_username
Everything I've tried with UNION seems to add the counts together, or removes rows for some reason. Any ideas?
Do a subquery to get each total, combine them with UNION, and then merge them with SUM().
SELECT SUM(count_in) count_in, SUM(count_out) count_out, fname
FROM (SELECT COUNT(*) count_in, 0 count_out, in_username fname
FROM visits v
GROUP BY fname
UNION
SELECT 0 count_in, COUNT(*) count_out, out_username fname
FROM visits v
GROUP BY fname) combined

Hibernate select relevent data in one query

I am new to Hibernate.
This is a User table
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(16) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
This is my sample data
+----+-------+------+------+
| id | name | age | sex |
+----+-------+------+------+
| 1 | Amit | 20 | M |
| 2 | Sumit | 21 | M |
| 3 | Mohan | 22 | M |
| 4 | Ram | 26 | M |
| 5 | John | 22 | M |
| 6 | Sita | 19 | F |
+----+-------+------+------+
These are my queries:
select id from User where name="Amit" and age=20;
select id from User where name="Ram" and age=26;
select id from User where name="Mohan" and age=22;
select id from User where name="Sita" and age=19;
I can do this in hibernate by running 4 queries.
Is there any way by which I can do this in just one query?
Please tell the same can be done in mysql too.
Please help. Any clue, link or reference will be appreciable.
Thank you.
you can do this
select id , name ,age , sex from User
where (name,age) IN (('Amit','20'),('Ram', '26'),('Mohan', '22'),('Sita', '19'));
DEMO HERE
In sql ...
select name, id from User where ( name="Amit" and age=20)
or ( name="Ram" and age=26 )
or ( name="Mohan" and age=22 )
or ( name="Sita" and age=19 )

Doubts about queries in MySQL

I wanted to make a query with Rails, something like this:
filters = Filter.joins(:category_filters).where("category_id IN (?)", params[:categories]).group("filters.id")
And the MySQL statement that is making is this:
SELECT `filters`.* FROM `filters` INNER JOIN `category_filters` ON `category_filters`.`filter_id` = `filters`.`id` WHERE (category_id IN ('9,4')) GROUP BY filters.id
At first sight, this query is ok, but when I look the results, its wrong. Let me explain you.
First, this is a query to the table filters:
select * from filters;
+----+----------+-------+----------+------------+------------+
| id | name | other | optional | created_at | updated_at |
+----+----------+-------+----------+------------+------------+
| 1 | material | 1 | 1 | NULL | NULL |
| 2 | abc | 1 | 0 | NULL | NULL |
| 3 | xyz | 0 | 0 | NULL | NULL |
| 4 | 123a | 0 | 0 | NULL | NULL |
+----+----------+-------+----------+------------+------------+
Second, this is a query to the table category_filters:
select * from category_filters;
+----+-----------+-------------+------------+------------+
| id | filter_id | category_id | created_at | updated_at |
+----+-----------+-------------+------------+------------+
| 1 | 1 | 1 | NULL | NULL |
| 2 | 2 | 1 | NULL | NULL |
| 3 | 1 | 9 | NULL | NULL |
| 4 | 2 | 9 | NULL | NULL |
| 5 | 1 | 4 | NULL | NULL |
| 6 | 3 | 4 | NULL | NULL |
+----+-----------+-------------+------------+------------+
And now, the query generated by Rails (the first query):
SELECT `filters`.* FROM `filters` INNER JOIN `category_filters` ON `category_filters`.`filter_id` = `filters`.`id` WHERE (category_id IN ('9,4')) GROUP BY filters.id;
+----+----------+-------+----------+------------+------------+
| id | name | other | optional | created_at | updated_at |
+----+----------+-------+----------+------------+------------+
| 1 | material | 1 | 1 | NULL | NULL |
| 2 | abc | 1 | 0 | NULL | NULL |
+----+----------+-------+----------+------------+------------+
Why is this happening?
But now, this is similar query, instead of using IN I used OR, like this:
SELECT `filters`.* FROM `filters` INNER JOIN `category_filters` ON `category_filters`.`filter_id` = `filters`.`id` WHERE (category_filters.category_id=9 or category_filters.category_id=4) GROUP BY filters.id;
+----+----------+-------+----------+------------+------------+
| id | name | other | optional | created_at | updated_at |
+----+----------+-------+----------+------------+------------+
| 1 | material | 1 | 1 | NULL | NULL |
| 2 | abc | 1 | 0 | NULL | NULL |
| 3 | xyz | 0 | 0 | NULL | NULL |
+----+----------+-------+----------+------------+------------+
What is happening?
In your WHERE clause, try this (assuming category_id is a number):
category_id IN (9,4)
else this (assuming category_id is a string)
category_id IN ('9','4')
instead of this (in your original query)
category_id IN ('9,4')
If you just do
where(:category_id => params[:categories]
Rails will create the proper SQL syntax for you