Asked  7 Months ago    Answers:  5   Viewed   27 times

I have three tables named

**Student Table**
-------------
id    name
-------------
1     ali
2     ahmed
3     john
4     king

**Course Table**
-------------
id    name
-------------
1     physic
2     maths
3     computer
4     chemistry

**Bridge**
-------------
sid    cid
-------------
1     1
1     2
1     3
1     4
2     1
2     2
3     3
3     4
4     1
4     2

Now to show the student name with the course name which he had studied like,

**Result**
---------------------------
Student        Course
---------------------------
ahmed         physic
ahmed         maths
ahmed         computer
ahmed         chemistry
ali           physic
ali           maths
john          computer
john          chemistry
king          physic
king          maths

I build following query

select s.name as Student, c.name as Course from student s, course c join bridge b on c.id = b.cid order by s.name

But it does not return the required result...

And what would be for normalized form, if I want to find who is manager over other:

**employee**
-------------------
id        name
-------------------
1         ali
2         king
3         mak
4         sam
5         jon

**manage**
--------------
mid      eid
--------------
1         2
1         3
3         4
4         5

And wants to get this result:

**result**
--------------------
Manager      Staff
--------------------
ali          king
ali          mak
mak          sam
sam          jon

 Answers

56

Simply use:

select s.name "Student", c.name "Course"
from student s, bridge b, course c
where b.sid = s.sid and b.cid = c.cid 
Tuesday, June 1, 2021
 
MDDY
answered 7 Months ago
96
SELECT `manufacturers`.*, `languages`.*, COUNT(`products`.`id`) AS NumberOfProducts
FROM (`manufacturers`)
JOIN `languages` ON `manufacturers`.`lang` = `languages`.`id`
LEFT OUTER JOIN `products` ON 
      `products`.`manufacturerid` =  `manufacturers`.`manufacturerid`
GROUP BY <Column list for manufacturers AND languages here>
ORDER BY `languages`.`id` asc, `manufacturers`.`id` asc
Wednesday, March 31, 2021
 
iceduck
answered 9 Months ago
94

You do not need to do that. You are using prepared statements, which escape the variables automatically.

Wednesday, March 31, 2021
 
laurent
answered 9 Months ago
57

You mean like this:

select
    a.project_id,
    b.list_id,
    c.item_id
from
    projects a
        join lists b
            on a.project_id=b.project_id
        join items c
            on b.list_id=c.list_id

Output will be something like:

project_id | list_id | item_id
 1         | 5       |  45
 1         | 5       |  46
 1         | 8       |  12

Or did you want to return all the parts of it in a single row?

If you want a single row, you can do something along the lines of:

select
    a.project_id,
    group_concat(b.list_id) as listIDs,
    group_concat(c.item_id) as itemIDs
from
    projects a
        join lists b
            on a.project_id=b.project_id
        join items c
            on b.list_id=c.list_id

But it will get messier in PHP to deal with all the grouped stuff.

Output will be something like:

project_id | list_id | item_id
 1         | 5,8     |  45, 46, 12

You could also mix and match the two to maybe get the best of both worlds:

select
    a.project_id,
    b.list_id as listIDs,
    group_concat(c.item_id) as itemIDs
from
    projects a
        join lists b
            on a.project_id=b.project_id
        join items c
            on b.list_id=c.list_id

Output will be something like:

project_id | list_id | item_id
 1         | 5       |  45, 46
 1         | 8       |  12
Saturday, May 29, 2021
 
dzm
answered 7 Months ago
dzm
81

When you use just "localhost" the MySQL client library tries to use a Unix domain socket for the connection instead of a TCP/IP connection. The error is telling you that the socket, called MySQL, cannot be used to make the connection, probably because it does not exist (error number 2).

From the MySQL Documentation:

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option.

There are a few ways to solve this problem.

  1. You can just use TCP/IP instead of the Unix socket. You would do this by using 127.0.0.1 instead of localhost when you connect. The Unix socket might by faster and safer to use, though.
  2. You can change the socket in php.ini: open the MySQL configuration file my.cnf to find where MySQL creates the socket, and set PHP's mysqli.default_socket to that path. On my system it's /var/run/mysqld/mysqld.sock.
  3. Configure the socket directly in the PHP script when opening the connection. For example:

    $db = new MySQLi('localhost', 'kamil', '***', '', 0, 
                                  '/var/run/mysqld/mysqld.sock')
    
Tuesday, June 1, 2021
 
Anand
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share