In documents specified that with SPH_SORT_TIME_SEGMENTS i can sort my timestamp DESCENDED, but how i can sort my timestamp ASCENDED? i trying using SPH_SORT_ATTR_ASC but the it shows null results. i tried it using "modified_date" (using sql_attr_timestamp) and "integered_modified_date" (using sql_attr_bigint) and both of it still showing no results
here's my configuration
source member_private_messages {
type = mysql
sql_host = 192.168.7.101
sql_user = root
sql_pass =
sql_db = proto_db
sql_query = SELECT \
b.message_id as primary_message_id, \
b.message_id as message_id, \
CONCAT(a.first_name, ' ',a.last_name) AS `from` , \
CONCAT(c.first_name, ' ',c.last_name) AS `to` , \
message_from_id, \
deliver_object_id, \
message_title, \
message_content, \
UNIX_TIMESTAMP(modified_date) AS modified_date, \
UNIX_TIMESTAMP(modified_date) AS integered_modified_date \
FROM tbl_member a JOIN \
tbl_messages b JOIN \
tbl_member c ON a.register_id = b.message_from_id AND b.deliver_object_id = c.register_id
# FOR SORT BY or FILTER
sql_attr_bigint = message_id
sql_attr_bigint = message_from_id
sql_attr_bigint = deliver_object_id
sql_attr_string = message_title
sql_attr_string = message_content
sql_attr_timestamp = modified_date
sql_attr_bigint = integered_modified_date
}
here is my php
<?php
require_once "sphinxapi.php";
$client = new SphinxClient();
$client->SetServer('localhost', 9312);
$client->SetConnectTimeout(30);
$client->SetArrayResult(true);
// Sort the index
$client->SetMatchMode(SPH_SORT_ATTR_ASC ,'modified_date'); //RETURNS NULL
// Query the index
$results = $client->Query('','member_private_messages_index');
// Output the matched results in raw format
var_dump($results['matches']);
here is my database structure (it haves 2 simple data)
CREATE TABLE `tbl_member` (
`member_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`register_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`register_type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`first_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`last_name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`registration_date` datetime DEFAULT NULL,
`verification_date` datetime DEFAULT NULL,
`update_date` datetime DEFAULT NULL,
PRIMARY KEY (`member_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
CREATE TABLE `tbl_messages` (
`message_id` int(255) unsigned NOT NULL AUTO_INCREMENT,
`message_from_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`message_from_type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`is_public` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL,
`message_type` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`deliver_object_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`deliver_object_type` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
`message_title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
`message_content` blob,
`created_date` datetime DEFAULT NULL,
`modified_date` datetime DEFAULT NULL,
PRIMARY KEY (`message_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
$client->SetMatchMode(SPH_SORT_ATTR_ASC ,'modified_date'); //RETURNS NULL
Why are you trying to define a sorting mode via setMatchMode? surely should be using setSortMode
btw it shouldnt return anyting. Its just setting a value for use by the later Query.
Related
I would like some help on this, i have a table like this
CREATE TABLE IF NOT EXISTS `items` (
`id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`code` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
`unitPrice` decimal(8,2) NOT NULL,
`quantity` int(11) NOT NULL,
`totalSold` int(11) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
and after using DB::table('items')->get();
i would like to get the same result as if i run this sql command
"select `code`,(`unitPrice`*(`quantity`+`totalSold`)) as totalEarnIfsold from `items` order by `totalEarnIfsold` desc"
what i have been able to achieve without success ofc is this:
$items_all->sortBy([
$totalEarnIfsold= fn ($a) => $a->unitPrice *($a->quantity+$a->totalSold),
['totalEarnIfsold', 'desc'],
]);
So i need your help if you may ofc, and thanks
You can do in following way
First you need to calculate totalEarnIfsold with map function and after that you can easily sort with value.
$items_all = $items_all->map(function($item) {
$item->totalEarnIfsold = $item->unitPrice *($item->quantity + $item->totalSold);
return $item;
})->sortByDesc('totalEarnIfsold');
I am trying to create a Voyager user. I enter php artisan voyager:admin user#email.com --create in a Windows command window. It aborts because the enter user prompt did not wait for my input.
What do I need to do to make it wait for an answer? I am using Laravel 8.
This is a bug in PHP 7.4.0 on Windows so you either update to 7.4.1 or downgrade to 7.3.
I don't want to downgrade or upgrade the PHP version. So I simply solved the problem by executing these queries on the Database directly.
Password is: "password"
CREATE TABLE IF NOT EXISTS `admins` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`role_id` bigint(20) UNSIGNED DEFAULT NULL,
`name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
`avatar` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT 'users/default.png',
`password` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`remember_token` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`settings` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `admins_email_unique` (`email`),
KEY `admins_role_id_foreign` (`role_id`)
)
INSERT INTO `admins` (`id`, `role_id`, `name`, `email`, `avatar`, `password`,
`remember_token`, `settings`, `created_at`, `updated_at`) VALUES
(1, 1, 'Admin', 'admin#admin.com', 'users/default.png',
'$2y$10$QndDoDX9ccomsjWXvWu1.uRFA5iGMEBiq1A5NcdrtvxgBDW4IIsAq', NULL, NULL,
'2023-01-09 21:40:59', '2023-01-09 21:40:59');
Admin has the role_id 1 and granted all permission by default. But If you face the permission problem, sort it out by visiting the permission_role table.
I am trying to write a query in my controller to display Quiz result for students. I have these tables
CREATE TABLE IF NOT EXISTS `quizz_question` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`topic` varchar(1000) COLLATE utf8_unicode_ci NOT NULL,
`question_code` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`answer1` varchar(100) COLLATE utf8_unicode_ci NULL,
`answer2` varchar(100) COLLATE utf8_unicode_ci NULL,
`answer3` varchar(100) COLLATE utf8_unicode_ci NULL,
`answer4` varchar(100) COLLATE utf8_unicode_ci NULL,
`topic` varchar(1000) COLLATE utf8_unicode_ci NOT NULL,
`correct_answer` varchar(100) COLLATE utf8_unicode_ci NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10 ;
CREATE TABLE IF NOT EXISTS `quizz_attempt` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`student_code` varchar(1000) COLLATE utf8_unicode_ci NOT NULL,
`answer` varchar(100) COLLATE utf8_unicode_ci NULL,
`question_code` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=10 ;
The two tables became these two model classes: QuizzQuestion and QuizAttempt.
In quizz_attempt, if a student select an answer (answer), it will take answer and question_code in quizz_attempt and compare with quizz_question.
quizz_attempt.answer = 1 then it will choose the content in answer1 as correct answer
quizz_attempt.answer = 2 then it will choose the content in answer2 as correct answer
quizz_attempt.answer = 3 then it will choose the content in answer3 as correct answer
quizz_attempt.answer = 4 then it will choose the content in answer4 as correct answer
public function gameQualifiers(Request $request)
{
$revenuedetails = DB::table('quizz_attempt as g')
->select(
'g.student_code',
'g.answer'
)
->orderByRaw('g.created_at DESC');
}
I know I need to join the two tables for the result. I started the code in my controller, but don't know how to complete it. i want to write a query to display list of students who choose correct answers
The quiz attempt should have the quizz_question_id as a Foreign Key. This will make it easier down the line to have the two connected.
You can set up two models to match your database tables: QuizzQuestion and QuizzAttempt. You can set up Foreign Keys like so:
QuizzAttempt.php
public function quizzQuestion()
{
return $this->belongsTo('App\QuizzQuestion');
}
And QuizzQuestion.php
public function quizzAttempts()
{
return $this->hasMany('App\QuizzQuestion');
}
So now, you want to get all of the attempts where the answer is correct - you are looking at an instance of QuizzQuestion, e.g.
$question = QuizzQuestion::find(1); // first question
$correctResults = QuizzAttempts::where('quizz_question_id', $question->id)
->where('answer', $question->correct_answer)
->pluck('student_code');`
Now you have all the student codes of the students that got the correct answers.
*** Update
If you cannot change the structure of the tables, you can run the following query:
// 1) find the question you want the answers for
$question = QuizzQuestion::find(1);
// 2) retrieve the correct results
$correctResults = QuizzAttempts::where('question_code', $question->question_code)
->where('answer', $question->correct_answer)
->pluck('student_code');`
Here is my SQL (with explain):
EXPLAIN
UPDATE
VF_KRED kred
INNER JOIN GBI gbi ON
kred.vendor = gbi.vendor
INNER JOIN MASTER_DATA master_data ON
master_data.vendor_code = gbi.vendor
SET
kred.GBI_VAL_REP =
(CASE
WHEN kred.country = 'JP' THEN
CASE
WHEN gbi.WITHHOLD_TAX_CODE = 0 THEN 2
WHEN gbi.WITHHOLD_TAX_CODE = 'IN' THEN 1
END
ELSE
CASE
WHEN gbi.WITHHOLD_TAX_CODE = master_data.REDUCED_RATE THEN
CASE
WHEN gbi.WITHHOLD_TAX_CODE = 0 THEN 3
WHEN gbi.WITHHOLD_TAX_CODE BETWEEN 1 AND 19 THEN 4
END
ELSE 5
END
END);
Giving below the outcome:
Now, the UPDATE SQL takes ~6+ minutes.
Below are the indexes on tables:
VF_KRED table:
GBI table:
MASTER_DATA table:
VENDOR columns are all varchar(25) type.
-- EDIT: (describing whole table structure below):
SHOW CREATE TABLE GBI;
CREATE TABLE `GBI` (
`VENDOR` varchar(25) CHARACTER SET utf8 NOT NULL,
`COMPANY_CODE` varchar(5) CHARACTER SET latin1 NOT NULL DEFAULT '',
`VENDOR_NAME` varchar(100) CHARACTER SET utf8 NOT NULL DEFAULT '',
`WITHHOLD_TAX_TYPE` varchar(2) CHARACTER SET latin1 NOT NULL DEFAULT '',
`WITHHOLD_TAX_CODE` varchar(2) CHARACTER SET latin1 NOT NULL DEFAULT '',
`DOCUMENT_CURRENCY` varchar(4) CHARACTER SET latin1 NOT NULL DEFAULT '',
`CREATE_DT` datetime NOT NULL,
PRIMARY KEY (`VENDOR`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SHOW CREATE TABLE VF_KRED;
CREATE TABLE `VF_KRED` (
`VENDOR` varchar(25) CHARACTER SET latin1 NOT NULL,
`COUNTRY_CODE` varchar(5) CHARACTER SET latin1 DEFAULT NULL,
`COUNTRY` varchar(3) CHARACTER SET latin1 DEFAULT NULL,
`SEARCH_TERM` varchar(100) CHARACTER SET latin1 DEFAULT NULL,
`CREATE_DT` datetime DEFAULT NULL,
`GBI_VAL_REP` int(11) NOT NULL DEFAULT 5,
PRIMARY KEY (`VENDOR`),
KEY `vf_kred` (`GBI_VAL_REP`),
KEY `VF_KRED_COUNTRY_IDX` (`COUNTRY`) USING BTREE,
CONSTRAINT `vf_kred_ibfk_1` FOREIGN KEY (`GBI_VAL_REP`) REFERENCES `VENDOR_RATE_CATEGORY` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
SHOW CREATE TABLE MASTER_DATA;
CREATE TABLE `MASTER_DATA` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`VENDOR_CODE` varchar(25) CHARACTER SET latin1 DEFAULT NULL,
`VENDOR_NAME` varchar(2000) CHARACTER SET latin1 DEFAULT NULL,
`COUNTRY` varchar(2) CHARACTER SET latin1 DEFAULT NULL,
`APPLN_RECEIVED_FROM_US` datetime DEFAULT NULL,
`ITUNES_CONTRACT_NUM` varchar(2000) CHARACTER SET latin1 DEFAULT NULL,
`CONTRACT_EFFECTIVE_DT` datetime DEFAULT NULL,
`CONTRACT_EXPIRATION_DT` datetime DEFAULT NULL,
`AUTO_RENEWAL` varchar(10) CHARACTER SET latin1 DEFAULT NULL,
`REDUCED_RATE` int(11) DEFAULT NULL,
`FORM_17` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
`RESIDENCY_CERT_ISSUE_DT` datetime DEFAULT NULL,
`TAX_AUTHORITY_SUBMISSION_DT` datetime DEFAULT NULL,
`TAX_AUTHORITY_ACCEPTANCE_DT` datetime DEFAULT NULL,
`STATUS` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
`FORM_17_EXPIRATION` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
`COMMENTS` varchar(2000) CHARACTER SET latin1 DEFAULT NULL,
`REMARKS` varchar(2000) CHARACTER SET latin1 DEFAULT NULL,
`CATEGORY` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
PRIMARY KEY (`ID`),
KEY `MASTER_DATA_VENDOR_CODE_IDX` (`VENDOR_CODE`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2369 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
Any clues?
Do not mix CHARACTER SETs (or datatypes or collations) on columns that need to be compared, as in a JOIN. Note how vendor is latin1 versus utf8. This slows down the JOIN. (Also, check vendor_code due to another join.)
The clue in EXPLAIN: "func". This implied that somehow vendor was being modified in order to perform the JOIN. The CREATE TABLE showed that the charset was different.
128MB for innodb_buffer_pool_size is find, even advisable, if you have only 1GB of RAM. For 8GB, 6G would be a better setting. By increasing the value, you will probably cut back on the I/O necessary to run this, and other, queries. Again, this impacts the speed of the JOIN.
Hello all i have a problem that when i try to join my comment table it show nothing
here is my code
$this->db->select('*,users.profil_billed as profil_billed, forum_traad.id as traad_id');
$this->db->from('forum_traad');
$this->db->join('users', 'forum_traad.brugernavn = users.username');
$this->db->where('forum_traad.fk_forum_kategori', $id);
$this->db->join('forum_kommentare', 'forum_traad.id = forum_kommentare.fk_forum_traad');
$this->db->where('forum_kommentare.fk_forum_traad', 'forum_traad.id');
$this->db->order_by("forum_traad.id", "DESC");
its then i put this in it show nothing, and i dont know why.. do i need some left join, right join etc.? i have try all of it :d
$this->db->join('forum_kommentare', 'forum_traad.id = forum_kommentare.fk_forum_traad');
$this->db->where('forum_kommentare.fk_forum_traad', 'forum_traad.id');
kommentare = comments
traad = thread
brugernavn = username
kategori = categori
its danish,
sorry my bad english hope some one can help me out
my database structure is this and im using mysql
CREATE TABLE `forum_kategori` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`kategori` text NOT NULL,
`beskrivelse` mediumtext NOT NULL,
`godkendt` varchar(4) NOT NULL DEFAULT 'ja',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10;
CREATE TABLE `forum_kommentare` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fk_forum_traad` int(11) NOT NULL,
`brugernavn` text NOT NULL,
`indhold` mediumtext NOT NULL,
`dato` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`time` text NOT NULL,
`godkendt` varchar(4) NOT NULL DEFAULT 'ja',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=28;
CREATE TABLE `forum_traad` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`overskrift` text NOT NULL,
`indhold` mediumtext NOT NULL,
`fk_forum_kategori` int(11) NOT NULL,
`brugernavn` text NOT NULL,
`dato` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`godkendt` varchar(4) NOT NULL DEFAULT 'ja',
`time` text NOT NULL,
`status` varchar(8) NOT NULL DEFAULT 'aaben',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=4;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(80) NOT NULL,
`password` text NOT NULL,
`kon` varchar(5) NOT NULL,
`alder` text NOT NULL,
`hood` varchar(4) DEFAULT NULL,
`fornavn` varchar(60) DEFAULT NULL,
`efternavn` varchar(100) DEFAULT NULL,
`city` text,
`ip` varchar(20) DEFAULT NULL,
`level` text,
`email` text,
`point` int(11) NOT NULL,
`oprettet` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`fritekst` mediumtext NOT NULL,
`profil_billed` text NOT NULL,
`online` varchar(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=31;
and it return nothing and no errors it only show a message i have done with a else statement that said "there is no threads on this categori", i have try with left join, right join inner join full join, ye all of it :S
You should try using a left join on your joins and work from there. If that doesn't work then there isn't a link between the tables.
Try this code:
$this->db->select('*,users.profil_billed as profil_billed, forum_traad.id as traad_id');
$this->db->from('forum_traad');
$this->db->join('users', 'forum_traad.brugernavn = users.username');
$this->db->join('forum_kommentare', 'forum_traad.id = forum_kommentare.fk_forum_traad');
$this->db->where('forum_traad.fk_forum_kategori', $id);
$this->db->order_by("forum_traad.id", "DESC");
You can use after $this->db->get() the
print $this->db->last_query();
and check what's wrong.