I have the following as my results.. the problem is I only want 1 field from the balances table and 2 fields from the userinfo table... if I replace the * with say user,avatar I get my error...
$result = mysql_query("SELECT * FROM userinfo INNER JOIN balances ON userinfo.user = balances.user ORDER By balance DESC,avatar");
if (!$result) {
die("Query to show fields from table failed");
I do not know the proper form and cant find it anywhere
TIA
John
as user column exists in both the joined tables userinfo and balances you need to prefix the table name while accessing the column
Try
SELECT userinfo.user, userinfo.avatar, balances.balance
FROM userinfo
INNER JOIN balances
ON userinfo.user = balances.user
ORDER By balance DESC,avatar
Related
I have two tables, one for registered users and one to store votes.
We are logging in with registrants.id and registrants.zipcode. Once they vote their votes are inserted into the votes table, along with their Registration ID.
Im trying to right a select statement that returns a record that will select all the records for Matched ID and Zipcode, but the ID is not in the Votes.voter column. i have tried all kinds of variations of all the joins i can think of. is it something simple i am missing.
SELECT * FROM registrants
LEFT JOIN votes on registrants.id = votes.voter
WHERE registrants.id = 1 AND registrants.zipcode = 46706 and votes.voter <> 1
Perhaps a not exists query:
select * from registrants
where registrants.zipcode = '46706'
and not exists (select 1 from votes where registrants.id = votes.voter)
I have table employees with columns: id, employee_id with values say ABC1234 and 4 respectively.
Another table cashadvance with columns :id, date_advance, employee_id, amount with values say 3, 2018-06-20, 5, 1500.
Now i want do sql select query in PHP to show date_advance and amount for the specific user/logged in employee(username is employee_id e.g, ABC1234 plus pswd) .
I have trouble cause employee_id in cashadvance is the primary key for employee in table employees. Please help
Below is the query written for your purpose to get those data for employees in employees table and related data in cashadvance . Here an inner join is used , please run the query and let me know if you face any error .
$sql = "select *
from employees as 'emp'
inner join cashadvance as 'cad'
on cad.employee_id = emp.employee_id
where 1" ;
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 have 2 table
Table-1 - user
user(ID.Name,Class)
Table-2 - Category
Category(ID,user_id,cat_id)
if user input a data from text field the how to search data from both table
Just query both tables and use the 'OR' operator for the columns you want to search in
SELECT * from user, category WHERE user.id=[text field] or category.user_id=[text field] or category.cat_id=[text field]
PHP Example: (assuming you are using MySQL database - you also need mysqli enabled in your php.ini file)
$mysqli = mysqli_connect(HOSTNAME, USERNAME, PASSWORD, DATABASE);
if (mysqli_connect_errno($mysqli)) {throw new exception("Failed to connect to MySQL: " . mysqli_connect_error());}
$sql = " SELECT * from user, category WHERE user.id='".$text_field."' or category.user_id='".$text_field."' or category.cat_id='".$text_field."'";
$rows = $result->fetch_array(MYSQLI_ASSOC);
foreach($rows as $row){
print_r($row);
}
This should get the records and show you the response from the array.
I hope you have added primary key & foreign key relations to these tables, in-order to grab data from both the tables you have two ways, either do multiplication of both table and bring in all the data else make use of JOIN that does the same in efficient way
Hoping to have your table schema as
USER [table] having id, username, name, password
CATEGORY [table] having id, name, description, user_id
So the query will become
SELECT U.*, C.id as cat_id, C.name as cat_name, C.description as cat_desc
FROM USER U
JOIN CATEGORY C ON C.user_id = U.id
If you have user inputting data from input fields, that becomes a filter query to be added in our above query, assume user is entering Name of category and wants the result set of the same then the above query gets added with WHERE clause as below
WHERE C.name LIKE '%{INPUT FIELD CONTENT HERE}%'
I have used LIKE clause above to allow us doing PARTIAL search
Hope this helps you
I have two tables. First table is je_addchoice, which contains fields like
choiceid
pollid
choicename
choicecreatorid
and the second table is je_uservote and the fields are
userid
pollid
choiceid
What i want to do is,
Display the choice names based on the no of votes in the je_uservote table
$query = select * from je_addchoice where poll_id='$poll_id' //order by (count(choiceid)) from second table
//QUERY FOR DISPLAY CHOICENAMES BASED ON COUNT OF VOTES
How to write the above query
My question is how to access the no of counts in the jeuservote table and display the choicenames based on the result count. Actually the votes for the choicenames in the addchoice table count is stored in the jeuservote table. How can i access the vote count for the choice names
SELECT *, (
SELECT count(*)
FROM je_uservote T2
WHERE T2.pollid=T1.pollID
AND T2.choiceid=T1.choiceID) AS votes
FROM je_addchoice T1
ORDER BY votes