I want to join 3 table with accounts, mailboxes, mailbox_edi_profile.
I join accounts to mailboxes with accounts.id and mailboxes.account_id and join mailboxes to mailbox_edi_profile whit mailboxes.id and mailbox_edi_profile.mailbox_id
I have query in SQL like this.
SELECT
accounts.id,
NAME,
GROUP_CONCAT(
mailbox_with_profile_nos SEPARATOR "<br>"
) AS mailbox_with_profile_nos
FROM
accounts
LEFT JOIN(
SELECT
mailboxes.id,
account_id,
IF(
GROUP_CONCAT(
mailbox_edi_profile.profile_no SEPARATOR "<br>"
) IS NULL,
username,
CONCAT(
username,
" (",
GROUP_CONCAT(mailbox_edi_profile.profile_no),
")"
)
) AS mailbox_with_profile_nos
FROM
mailboxes
LEFT JOIN mailbox_edi_profile ON mailbox_edi_profile.mailbox_id = mailboxes.id
GROUP BY
mailboxes.id
) AS mailboxes
ON
accounts.id = mailboxes.account_id
GROUP BY
accounts.id;
I try to this.
$list = DB::table('accounts')
->select('accounts.id', 'NAME', DB::raw('GROUP_CONCAT(mailbox_with_profile_nos SEPARATOR "<br>") as mailbox_with_profile_nos'))
->leftJoin(DB::raw('(SELECT mailboxes.id, account_id, IF(GROUP_CONCAT(mailbox_edi_profile.profile_no SEPARATOR "<br>") IS NULL, username, CONCAT(username," (",GROUP_CONCAT(mailbox_edi_profile.profile_no),")")) as mailbox_with_profile_nos FROM mailboxes LEFT JOIN mailbox_edi_profile ON mailbox_edi_profile.mailbox_id = mailboxes.id GROUP BY mailboxes.id) mailboxes'), 'accounts.id', '=', 'mailboxes.account_id')
->groupBy('accounts.id', 'mailboxes.account_id');
I get this error , but not work
SQLSTATE[42000]: Syntax error or access violation: 1055 'laravel.mailboxes.account_id' isn't in GROUP BY (SQL: select count(*) as aggregate from (select `accounts`.`id`, `NAME`, GROUP_CONCAT(mailbox_with_profile_nos SEPARATOR "<br>") as mailbox_with_profile_nos from `accounts` left join (SELECT mailboxes.id, account_id, IF(GROUP_CONCAT(mailbox_edi_profile.profile_no SEPARATOR "<br>") IS NULL, username, CONCAT(username," (",GROUP_CONCAT(mailbox_edi_profile.profile_no),")")) as mailbox_with_profile_nos FROM mailboxes LEFT JOIN mailbox_edi_profile ON mailbox_edi_profile.mailbox_id = mailboxes.id GROUP BY mailboxes.id) mailboxes on `accounts`.`id` = `mailboxes`.`account_id` group by `accounts`.`id`, `mailboxes`.`account_id`) as `aggregate_table`)
It may be related to mysql mode ONLY_FULL_GROUP_BY, which obliges you to have all selected columns in group by. To remove this mode you can access file config/database.php and comment ONLY_FULL_GROUP_BY in mysql modes :
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'modes' => [
//'ONLY_FULL_GROUP_BY', HERE
'STRICT_TRANS_TABLES',
'NO_ZERO_IN_DATE',
'NO_ZERO_DATE',
'ERROR_FOR_DIVISION_BY_ZERO',
'NO_AUTO_CREATE_USER',
'NO_ENGINE_SUBSTITUTION'
],
'engine' => 'InnoDB ROW_FORMAT=DYNAMIC',
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Related
My Laravel 8 project is using mainDB defined in .env file. I also defined an second external database as extDB.
DB_HOST=server1
DB_DATABASE=mainDB
DB_USERNAME=user
DB_PASSWORD=pswd
DB_EXT_HOST=server2
DB_EXT_DATABASE=extDB
DB_EXT_USERNAME=user
DB_EXT_PASSWORD=pswd
As well as in the config/database.php
'connections' => [
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'mysql_external' => [
'driver' => 'mysql',
'host' => env('DB_EXT_HOST', 'localhost'),
'database' => env('DB_EXT_DATABASE', 'forge'),
'username' => env('DB_EXT_USERNAME', 'forge'),
'password' => env('DB_EXT_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
]
I can get data from extDB using the following code:
$db_ext = DB::connection('mysql_external');
$images = $db_ext->table('GW_xImages')->get();
But how can I execute such query to sync both tables which have different fields?
REPLACE INTO mainDB.images (id, created_at, updated_at, is_active, rating, title, caption, summary, location, subregion, region, country_id, gps, keywords, author, note, date, camera_body, camera_lens, camera_focal, camera_iso, camera_speed, camera_aperture) SELECT Image_ID, Date_Created, Date_Modified, Mark_Active, TotalStars, ImageTitle, Image, ImageNewTitle, Location, SubRegion, Region, Country_ID, LocationGPS, Keywords, Author, Note, Image_Date, Camera_Body, Camera_Lens, Camera_Focale, Camera_ISO, Camera_Speed, Camera_Aperture FROM extDB.GW_xImage WHERE Date_Modified > NOW();
I got this piece of code bellow to show a table in a page, but it doesn't work. I got this error where says that the command was denied.
$posts = DB::connection('mysql2')
->table('wp_rocketsciencebrposts')
->join('users', 'users.rsbwordpressid', '=', 'wp_rocketsciencebrposts.post_author')
->select('ID', 'post_title', 'post_status', 'post_author', 'post_date', 'users.name')
->whereIn('post_status', ['publish', 'private'])
->where('post_type', 'post')
->orderBy('id', 'desc')
->paginate(15, ['*'], 'posts');
SQLSTATE[42000]: Syntax error or access violation: 1142 SELECT command
denied to user 'lovel095_heaven'#'177.134.6.23' for table 'users'
(SQL: select count(*) as aggregate from wp_rocketsciencebrposts
inner join mysql.users on users.rsbwordpressid =
mysql2.wp_rocketsciencebrposts.post_author where post_status
in (publish, private) and post_type = post)
Both databases are on the same server.
The table "wp_rocketsciencebrposts" comes from "lovel095_rocketsciencebr" database.
The table "users" comes from "lovel095_centralrsb" database.
In my .env file i git this:
DB_CONNECTION=mysql
DB_HOST=br862.hostgator.com.br
DB_PORT=3306
DB_DATABASE=lovel095_centralrsb
DB_USERNAME=(myusername)
DB_PASSWORD=(mypassword)
DB_CONNECTION_SECOND=mysql
DB_HOST_SECOND=br862.hostgator.com.br
DB_PORT_SECOND=3306
DB_DATABASE_SECOND=lovel095_rocketsciencebr
DB_USERNAME_SECOND=(myusername)
DB_PASSWORD_SECOND=(mypassword)
In my config>database.php file, i got this:
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
'mysql2' => [
'driver' => env('DB_CONNECTION_SECOND'),
'host' => env('DB_HOST_SECOND'),
'port' => env('DB_PORT_SECOND'),
'database' => env('DB_DATABASE_SECOND'),
'username' => env('DB_USERNAME_SECOND'),
'password' => env('DB_PASSWORD_SECOND'),
],
Pls, help!
Most of database is not support a cross database join, I believe SQL server support it but it must be on the same server. Instead of forcing your application to joining two different database, just simply use the database separately.
In your case, simply select the posts first, then select the user name using foreach. Here's the workaround :
$posts = DB::connection('mysql2')
->table('wp_rocketsciencebrposts')
->select('ID', 'post_title', 'post_status', 'post_author', 'post_date')
->whereIn('post_status', ['publish', 'private'])
->where('post_type', 'post')
->orderBy('id', 'desc')
->paginate(15, ['*'], 'posts');
foreach ($posts as $key => $post){
$user = DB::connection('mysql')
->table('lovel095_centralrsb')
->where('id', $post->post_author)
->first();
$posts[$key]->post_author_name = $user->name;
}
Hello I'm not sure you need to configure 2 databases to execute that kind of query.
Just make sure the credentials you are using to connect to one database is able to access both, it works with MySql.
From here i suggest you read that other stackoverflow answer
Basically you need to prefix tables with database name for the join to work.
I suggest you first try this from PhpMyAdmin then translate into Laravel Query Builder.
Remember that you can use entire raw queries in the Query builder, like :
DB::select( DB::raw("SELECT * FROM some_table WHERE some_col = '$someVariable'") );
I have written this query with pagination in it
$items = Item::select('items.*', 'sub_category_name', 'category_name', 'sub_category_slug', 'category_slug')
->join('sub_categories AS sc', 'sc.sc_id', 'items.sub_category_id')
->join('categories AS c', 'c.category_id', 'sc.category_id')
->where('items.is_active', '=', 1)
->where('sc.is_active', '=', 1)
->where('c.is_active', '=', 1)
->where('sc.sc_id', '=', $sub_category_id)
->paginate(1);
But it says
Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause
But when I add ->groupBy('item_id'); it says
Syntax error or access violation: 1055 'books.items.item_name' isn't in GROUP BY
But when I do item_name in groupBy clause it says to groupBy the next column. Why?
When you use aggregate functions like MIN(),MAX(),COUNT() AVG() you have to use Group BY
But in latest MYSQL u have to use all the columns in select as a group By too.
In your config/database.php turn off the strict mode.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
'strict' => false after that you can use group by on a single column too.
You can comment ONLY_FULL_GROUP_BY line in modes array
Here is my query, it works on all older versions.
$data = DB::table('applicant')
->groupBy('AppAffID')
->get();
Error i get is :
SQLSTATE[42000]: Syntax error or access violation: 1055 'comloant_loantreeAPI.applicant.AppID' isn't in GROUP BY (SQL: select * from `applicant` group by `AppAffID`)
I have tried the database change :
I added 'strict' => false, to database config.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
What is it that you want to achieve with the groupBy? If you want to sort on the AppAffID you should use the orderBy instead done like so:
$data = DB::table('applicant')
->orderBy('AppAffID', 'desc') //can also use 'asc' for ascending
->get();
This is the code that was working on laravel 5.2
$menus = CmsMenuItem::groupBy('menu_id')->get();
but now it throws error
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1
of SELECT list is not in GROUP BY clause and contains nonaggregated
column 'convertifier_cms.cms_menu_items.id' which is not functionally
dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by (SQL: select * from 'cms_menu_items' group
by 'menu_id')
I have also tried
`strict => false`
in database.php but no effect
Try this for database config.
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
and use query like this way
$menus =DB::table('cms_menu_item')
->select('*')
->groupBy('menu_id')
->get();
As per this PR just try this in your database config
'strict' => false,
If not there is some known issue is going on.
Please refer these links PR & Issue
Please Go to your config/database.php folder. In mysql configuration array, change strict => true to strict => false, and everything will work nicely.