I have two tables, the one called m_loan and the another one called m_action. I want to select datas from m_loan with criteria taken from the m_action
Here are the tables structure.
m_loan
no
name
accno
par
frequency
m_action
no
par
freq_min
freq_max
action
for example if there is a criteria data in m_action
1, 1, 3, 4, confiscation of collateral
2, 1, 1, 2, call the creditor
and there are some data in m_loan
1, Joko, 12345, 1, 2
the data above (Joko) will get the "call the creditor" respond because the PAR is 1 and the frequency is 2 (in the range of the 'call the creditor').
I have no idea how to to this. Thank you for your kind respond:)
select * from m_loan where m_loan.par=m_action.par and (m_loan.frequency between m_action.freq_min and m_action.freq_max)
Following sql will be helpful to you,
select ml.*, ma.action from m_loan ml, m_action ma
where ml.par = ma.par and (ml.frequency between ma.freq_min and ma.freq_max)
Related
PHP and MySQL
This is an existing project with a poor design and table layout.. that has been requested to have a new 'feature' added.
I'm wondering if a query like this can be put together (not the greatest with query stuff)
Quick Summary:
A form post is made.
table it gets a messageid and a (blank) subid
if a NEW post is made.. the above happens again..
(messageid and a (blank) subid)
if a 'comment' to an existing post is made..
table it gets a messageid and a subid that matches the 'parent' post/thread it is making a comment on
Unfortunately this is all in the same table.. and seems like it would have been better served in a multiple table layout or something?
But:
I know the parent posts (because the subid col is empty)
I know the child posts (because the subid col has value matching the parent post is a comment to)
All posts have a timestamp.
How can I get a query, and group/order things like:
Select -parent posts- without subid
immediately have all -child/comment posts- under this
And have it (initially?) order by the parent post_date then child/comment post (sub order by their post_dates)
Here is an example table set-up:
http://sqlfiddle.com/#!9/19dae/2/0
Not sure if a simple GROUP BY is needed here? Or a more advanced sub-select needs to be done?
The order needs to be 'first' done by parent post 'post_date'.. and (somehow) get the next child/comment posts to be -injected- after their 'parent' post in the query/array return..
Here is my desired outcome order:
Desired outcome:
'2020-03-30 09:48:50', 'Poster A', '49203-30032020094850000000', ''
'2020-03-30 09:50:50', 'Poster B', '49204-30032020095050000000', '49203-30032020094850000000'
'2020-04-03 09:55:50', 'Poster C', '49205-03042020095550000000', '49203-30032020094850000000'
'2020-03-31 09:48:50', 'Poster C', '49205-31032020094850000000', ''
'2020-04-03 09:40:50', 'Poster B', '49204-03042020094050000000', '49206-010432020094850000000'
'2020-04-01 09:48:50', 'Poster D', '49206-010432020094850000000', ''
'2020-04-02 09:49:50', 'Poster E', '49207-02042020094950000000', ''
update:
Addressing: #Ngo Van Quan
For his answer.
This:
select * from testtable
order by
(
CASE testtable.subid WHEN 0
THEN testtable.messageid*1000
ELSE testtable.subid*1000 + testtable.messageid END
),
testtable.messageid;
(which I think it what you intended to supply instead of what you posted??)
** does nothing
This query won't give you exactly what you're looking for, but will give you at least one row for every post. If there are no comments, you'll get one row with a bunch pf null info for comments. If there are comments you'll get one row per comment with the parent post information repeated:
SELECT *
FROM testtable AS post
LEFT JOIN testtable as `comment` ON `comment`.subid = post.messageid
WHERE post.subid = ''
ORDER BY post.postdate ASC;
Here is sql to do your tasks:
select * from table order by (CASE table.subid when 0 then table.id*1000 ELSE table.subid*1000+table.id END), table.id;
I have a earnings column on my site, I need to display the entire amount of earnings using a query in the database, but there is a small problem.
The table bets_double has columns: game_id, sum, color, and userid. And in the table game_double I have a column number it means winning number and id. In controller i bind numbers to colors this way:
$red = [1, 2, 3, 4, 5, 6, 7];
$black = [8, 9, 10, 11, 12, 13, 14];
$zero = [0];
How can I make a request in Laravel so that from the table bets_double take value color and checked the value from the table game_double and the total amount of victories was displayed?
For example, if in bets_double write color red, and in game_double number from 1 to 7 as write in $red variable, then to display the total winning. How to do it?
An example request code, but I don’t know how to send the values of variables in this code:
$double_games_money = DB::table('bets_double')
->where('user_id', $this->user->id)
->whereExists(function ($query) {
$query
->selectRaw(1)
->from('game_double')
->whereRaw('game_double.id = bets_double.game_id')
->whereRaw('game_double.number = bets_double.color');
})
I have a database, that contains movies and every movie post category field is written this way: 8, 12, 16. This means, that movie is Action and Drama and etc. This is what I'm doing:
$fields = "id, xfields, category";
$query = $db->prepare("SELECT $fields FROM `dle_post` WHERE `category` IN ($subcatsStr) ORDER BY `date` DESC LIMIT :from, :to");
$query->bindParam(":from", $from);
$query->bindParam(":to", $count);
$query->execute();
So now I want to get all movies, that are action movies(category ID: 8). subcatsStr look like this:
$subcatsStr = "8";
Query finds only movies, that category starts with 8, e.g. movie, which category is 8,12,47,53 or 8,11,12,17,53. But when category is e.g 12,8, it doesn't find it. How can I make it work as I want?
EDIT:
I noticed find_in_set works only when subcatsStr is a single digit, e.g 8, but it doesn't, when it's e.g 8, 12, 16, 19. When user selects top category(e.g. movies), subcatsStr contains many values, that are categories and subcategories. Solution for that?
Use FIND_IN_SET()
SELECT * FROM dle_post
WHERE find_in_set('$subcatsStr', category) > 0
But actually it would be better to change your table design. Never store multiple values in one column!
Hello i Have tableuser, with column id and many more. now i want all id in string form concatenated with comma separation and appending prefix as A.
means suppose i have records with id 1, 2, 3, 4 etc
now i want result like A1,A2,A3 like this
i did it with my way but its too complex i want to do it with single query.
my code are as under its working fine.
$send_idstring='';
$qry="SELECT concat('A',id) as id FROM `admin` WHERE concat(fname,' ',lname) LIKE '%".addContent($searchVal)."%' ";
$send_id=mysql_query($qry);
while($row=mysql_fetch_assoc($send_id)){
$send_idstring.=$row['id'].',';
}
$send_idstring=trim($send_idstring, ",");
echo $send_idstring;
it gives me output as i want but i want another way to do it please suggest.
Try
SELECT GROUP_CONCAT(CONCAT('A', `id`) SEPARATOR ',') AS idList FROM `admin`;
http://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html#function_group-concat
In PHP, I have an array of 11 persons where just the ID of each person is given:
$persons = array(1, 3, 39, 72, 17, 20, 102, 99, 77, 2, 982);
In my MySQL database, there's a table containing detailed information about each person:
TABLE personInfo
- ID (integer)
- name (string)
- birth date (timestamp)
- weight (decimal)
- ...
PROBLEM:
So now, I want to select the matching name for each ID in the PHP array. I can only imagine two solutions to do this:
1. for-loop:
foreach ($persons as $person) {
$result = mysql_query("SELECT name FROM personInfo WHERE id = ".$person);
}
2. logical operator OR
$result = mysql_query("SELECT name FROM personInfo WHERE id = 1 OR id = 3 OR id = 39 OR ...");
Both solutions are slow, aren't they?
But if I had another MySQL table containing the IDs of the PHP array ...
TABLE ids
- ID (integer)
... I could use a join to make a really fast MySQL query, right?
$result = mysql_query("SELECT a.ID, b.name FROM ids AS a JOIN personInfo AS b ON a.ID = b.ID");
QUESTION:
Is all this correct so far? If yes: Why is this so? The MySQL query is faster if I have a second table? With only one table it is incredibly slow? What is the fastest way to solve my problem (selecting the names matching the IDs of the PHP array)?
If you have lots of ids (several hundreds or more), feeding the values into a temporary table and joining it is actually faster.
You may want to read this article:
Passing parameters in MySQL: IN list vs. temporary table
IF you have ID's you should just query the Id's you have. So
$result = mysql_query("SELECT name FROM personInfo WHERE id = 1 OR id = 3 OR id = 39 OR ...");
would be right, although I would use
$result = mysql_query("SELECT name FROM personInfo WHERE id IN (1,2,3,4....) )