I have such sql:
mysql_query("SELECT *
FROM car
LEFT JOIN client
ON car.CodeClient = client.Code
LEFT JOIN telephone
ON car.CodeClient = telephone.CodeClient
WHERE Marka Like '$Marka'
and Model Like '%$Model%'
and EngineVol Like '%$EngineVol%'
and EngineType Like '%$EngineType%'
and DateMade Like '%$DateMade%'
");
And I need to select CodeClient and TelephoneNumber from telephone table, but select first entry for every Client, not all telephone, but first. Grouping is not the solving!
wouldn't this work? Sorry if it's not what you're looking for, but the question is a little unclear:
ORDER BY car.id LIMIT 1
or just:
LIMIT 1
Related
I have a MySQL table from which I want to extract attendance information(Student Id, course/subject for attendance, date range,whether the student was present or not). I have written the following query:
SELECT
COUNT(a_id),
(
SELECT COUNT(*) FROM attendance
WHERE state = 'present'
AND `dater` BETWEEN '$a' AND '$b'
) AS Count,
stud_id
FROM attendance
WHERE
stud_id =(SELECT id FROM users WHERE NAME = '$stud')
Which is giving me the correct results, but when I change the student,its not giving me the correct count for the days recorded for present. Not mention that I have not yet added the course parameter into the query
The MySQL table is as follows:
I need help for the query to return the desired results(Count the accurate days present for each student, as well as adding the course parameter into the query so that the query will look for attendance records for a specific course, for a specific student, for a specified date range).
Looks like you want to seperate your queries:
Select (select count(*) from <database>.attendance where state = 'present' AND (dater between '$a' and '$b') AND name=(SELECT id FROM users WHERE NAME = '$stud')) as present, (select count(*) from <database>.attendance where state = 'absent' AND (dater between '$a' and '$b') AND name=(SELECT id FROM users WHERE NAME = '$stud')) as absent from <database>.attendance WHERE stud_id =(SELECT id FROM users WHERE NAME = '$stud');
try this :)
Resolved it using JOIN as follows:
SELECT u.id, a.stud_id, a.course_id, count(*) FROM attendance a
JOIN users u ON u.id=a.stud_id
JOIN courses c ON c.c_id=a.course_id
WHERE a.state='present' and dater between '2017-09-01' and '2017-09-14'
GROUP BY a.stud_id, a.course_id;
Thanks for your help.
I am trying to only show unique userIds (userIds are (0,1,2,3,4,5,6,7,8,9 etc...) for the query I am running. I tried using DISTINCT in my query, but it only shows me unique values of the rows that have 2 or more of the same userId.
Is there a way I can use php to only show the unique values. My weak points are arrays and it makes it more complicated because its using data from a MySQLi query.
Example right now I have with the query now (lets say its GROUP BY rentPaid DESC and the rent total is 800.00 for all users):
userID rentPaid rentMonth
2--------800.00------April
1--------500.00------April
3--------400.00------April
3--------400.00------April
1--------200.00------April
1--------100.00------April
Example desired output:
userID rentPaid rentMonth
2--------800.00------April
1--------500.00------April
3--------400.00------April
Can I do this with MYSQL because I tried DISTINCT and it wouldn't work, how about PHP?
Query:
SELECT
properties.*,
leases.*,
users.userId, users.primaryPhone,
CONCAT(users.userFirstName,' ',users.userLastName) AS user,
admins.adminName, payments.*
FROM
properties
LEFT JOIN leases ON properties.propertyId = leases.propertyId
LEFT JOIN assigned ON properties.propertyId = assigned.propertyId
LEFT JOIN admins ON assigned.adminId = admins.adminId
LEFT JOIN users ON properties.propertyId = users.propertyId
LEFT JOIN payments ON properties.propertyId = payments.propertyId
WHERE
payments.rentMonth = '$currentMonth' AND
payments.rentYear = '$currentYear'
Edit: Please excuse my formatting, this is my first post.
Edit: Added query....its long, but works lol. I only want unique userIds (no double or triple userIds etc...)
I suspect this is what you want:
SELECT userID, MAX(rentPaid) AS maxRentPaid, rentMonth
FROM yourTable
WHERE rentMonth = "April"
GROUP BY userID
ORDER BY maxRentPaid
I've been Googling for most of my day now but I can't seem to find the right answer. Maybe because I don't really know in which direction to look (join? exist?). What I have is 3 tables:
There is a table named 'groups' and 'languages' containing the actual group- and language data but that's not important right now.
The user should be able to generate a list with all members, depending on the selected groups and/or languages. The groups/languages that the user selected are saved in two separate array's containing the IDs ($aGroups and $aLangs).
What I want/need is to SELECT * FROM members WHERE ...
And that's where I got stuck. I've tried joins, I've tried IN(), I've tried EXIST but nothing seems to work right.
Any help is greatly appreciated!
If you want to select members by languages and groups, you can do something like this :
$query = "select * from members as m
left join group_members as gm on gm.member_id = m.id
left join language_members as lm on lm.member_id = m.id";
if(isset($aGroups)) {
$query .= " where group_id in (".implode(",", $aGroups).")";
if(isset($aLangs)) {
$query .= " and language_id in (".implode(",", $aLangs).")";
}
}
elseif(isset($aLangs)) {
$query .= " where language_id in (".implode(",", $aLangs).")";
}
I think you just need some brackets:
//assuming you have ids stored in $lang and $group
SELECT * FROM members WHERE (SELECT group_id FROM group_members WHERE member_id=members.id)='$group'
AND (SELECT lang_id FROM language_members WHERE member_id=members.id)='$lang'
The trick is "members.id", DB will call subquery for every member to find out if the condition for group and lang is met.
Select m.* FROM members m, group_members gm, language_members lm
WHERE
lm.member_id=m.id AND
m.id=gm.member_id AND
<<<< START YOUR OWN WHERE HERE.
I need to select statements where is fixed post id, group by user id and with latest date. Here is what i have:
$bids = "SELECT uid, Max(date_made), bid FROM ".$wpdb->prefix."auction_bids WHERE pid=$pid GROUP BY uid";
In this query is problem only with date, it returns first results, but i need last.
Here is the screen of my database:
You need to obtain the groupwise maximum:
SELECT uid, date_made, bid
FROM ${wpdb->prefix}auction_bids NATURAL JOIN (
SELECT uid, MAX(date_made) AS date_made
FROM ${wpdb->prefix}auction_bids
WHERE pid = $pid
GROUP BY uid
) AS t
WHERE pid = $pid
See it on sqlfiddle.
use min instead of max and grab the id, then join:
$bids = "SELECT ss.uid, ss.min, b.bid FROM ".$wpdb->prefix."auction_bids bid INNER JOIN (SELECT uid, MIN(date_made) as min FROM ".$wpdb->prefix."auction_bids WHERE pid=$pid GROUP BY uid) ss ON ss.uid = bid.uid";
It's not clear what you want from your English. Sorry.
Try this
"SELECT pre.uid, pre.date_made, pre.bid
FROM ".$wpdb->prefix." AS pre
WHERE pre.pid = $pid AND date_made =
(SELECT MAX(pre2.date_made) FROM ".$wpdb->prefix." AS pre2
WHERE pre2.uid = pre.uid)
GROUP BY pre.uid";
A bit complicated but efficient. Similar to eggyal's answer but I'm using theta style coding and hence easy to understand.
I am using this to get customer name for auto complete function.
$query = $db->query("SELECT orderr_customer_name FROM orderr WHERE orderr_customer_name LIKE '$queryString%' GROUP by orderr_customer_name LIMIT 10");
if($query) {
while ($result = $query ->fetch_object()) {
echo '<li onClick="fill(\''.$result->orderr_customer_name.'\');">'.$result->orderr_customer_name.'</li>';
How can i Select another table? this is the table i want to use
("SELECT customer_name FROM customer WHERE customer_name LIKE '$queryString%' GROUP by customer_name LIMIT 10");
Thanks alot.
You should look into something like that (Note that I can't test it now, this is only a guideline):
SELECT orderr.orderr_customer_name, customer.customer_name
FROM customer INNER JOIN orderr_customer_name ON orderr.orderr_customer_name = customer.customer_name
WHERE customer_name LIKE '$queryString%' GROUP by customer_name LIMIT 1
Pleasy read the doc: Here
It sounds like you're asking how to query and combine the results from two tables through the use of a single query. This can be accomplished rather easily with a UNION in MySQL.
(SELECT orderr_customer_name AS customer_name FROM orderr WHERE orderr_customer_name LIKE '$queryString%')
UNION
(SELECT customer_name FROM customer WHERE customer_name LIKE '$queryString%')
ORDER BY customer_name LIMIT 10
Don't forget to make sure you escape/sanitize $queryString, and that you have proper indexes on your tables. Also, in your specific example, it appears you might be duplicating customer names in at least one place; while some applications require this, you may also want to consider normalization - but that's another topic.